Font Awesome SVG with JavaScript is compatible with jQuery but requires some additional understanding and planning. Learn how SVG works and what you need to do to successfully integrate with jQuery.
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 7,864 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
These docs only apply if you are using the SVG + JS version of Font Awesome. If this seems too complex, you might consider switching to Web Fonts with CSS. Each technology in Font Awesome 5 has its which, and integrating with jQuery can be a little daunting. Nesting the SVG tags inside of the <i> tags might also help you out.
Nesting SVG tags within their parent
A quick way to alleviate 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:
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".
If you don't nest, here's what you need to know
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 7,864 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
It looks for DOM elements which resemble fas fa-coffee
It figures out things like the prefix (fas), the icon (coffee), and anything else of interest
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, and this example fails to 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>
This example works 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>
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.
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 7,864 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
<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>