We’ll cover how SVG works and what you need to integrate with jQuery successfully. Below we'll describe an easy option using nesting. If nesting doesn’t work for you, we've got some more information and alternate options that can get you going.
A quick way to reduce issues with libraries like jQuery is to allow the SVG tags to nest within the parent tag, which will allow event binding and other DOM manipulation to function.
First, we need to configure Font Awesome SVG with JavaScript to nest by adding data-auto-replace-svg="nest" to the core script:
This special marker is used by Font Awesome to mark elements that have been rendered to SVG icons. The characters "i2svg" are short for "i tags to SVG."
Alternatives to Nesting
Advertisement
Remove ads with a Pro plan!
A subscription to a Pro-level plan will remove all third-party advertisements on fontawesome.com.
And of course Pro-level plans come with…
All 26,233 icons in Font Awesome
5 Classic styles of every icon
3 Sharp styles of every icon
A Perpetual License to use Pro
Services and tools to make easy work of using icons
The Font Awesome javascript looks for Font Awesome HTML DOM elements by looking at CSS classes, like fas, fa-solid or fa-dragon.
It figures out things like the style prefix (fas or fa-solid), the icon (dragon), and any styling utilities on the element.
It schedules the replacement of the <i> element for the next convenient time.
When the browser is ready the <i> DOM element is replaced with a new <svg> element.
A comment is added below the new <svg> element representing the original HTML for convenience.
Why Is This Important?
Since elements are replaced, any bindings to the element will be lost!
The example below will not work:
<nav><ulstyle="list-style: none;margin: 0;padding: 0;"><li><iclass="fa fa-user fa-2x"></i></li></ul></nav><script>
document.addEventListener('DOMContentLoaded',function(){$('nav i').on('click',function(){// the i element will not exist once the icon is renderedalert('You will never see this');});});</script>
The example below will work because it is not dependent on the <i> element:
<nav><ulstyle="list-style: none;margin: 0;padding: 0;"><li><iclass="fa fa-user fa-2x"></i></li></ul></nav><script>
document.addEventListener('DOMContentLoaded',function(){$('nav li').on('click',function(){// we are letting the li bind to the eventalert('This works, though');});});</script>
Attaching Events to the Icon
If you absolutely need to attach events to the icon, you can use the
data-fa-i2svg attribute, but you need to allow for the dynamic creation of the
SVG element.
You can attach the event to an ancestor and filter for the icon:
Avoid creating JavaScript variable references to icons
Since Font Awesome is dynamically replacing and modifying your icons, any held reference to an icon will not work as expected.
This will not work and will leave you sad:
<nav><ul><li><iclass="fa fa-user fa-2x"></i></li></ul></nav><script>
document.addEventListener('DOMContentLoaded',function(){var icon =$('nav i')$('nav li').on('click','[data-fa-i2svg]',function(){
icon.addClass('active');// This has no effect, the original icon has been replaced});});</script>
Changing Icons by Changing Classes
Advertisement
Remove ads with a Pro plan!
A subscription to a Pro-level plan will remove all third-party advertisements on fontawesome.com.
And of course Pro-level plans come with…
All 26,233 icons in Font Awesome
5 Classic styles of every icon
3 Sharp styles of every icon
A Perpetual License to use Pro
Services and tools to make easy work of using icons
If you want to swap out an icon, you can do that by changing the classes on the element, like this:
<button>Open up <iclass="fa fa-angle-right"></i></button><script>
document.addEventListener('DOMContentLoaded',function(){$('button').on('click',function(){$(this).find('[data-fa-i2svg]').toggleClass('fa-angle-down').toggleClass('fa-angle-right');});});</script>