Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch between icons when using FontAwesome 5.0 SVG Framework

I'm looking to be able to switch between icons in Javascript while using the new FontAwesome SVG framework.

Previously in the old WebFont method, this was achieved by toggling or changing the class on the tag, however as these are now rendered as SVG's in the source code this no longer works.

Is there a way to do this without needing to render both SVG icons in source code and using additional classes/CSS to toggle display?

like image 805
Monbrey Avatar asked Sep 14 '17 04:09

Monbrey


People also ask

How do I overlay two font awesome icons?

To stack multiple icons, use the fa-stack class on the parent HTML element of the 2 icons you want to stack. Then add the fa-stack-1x class for the regularly sized icon and add the fa-stack-2x class for the larger icon. fa-inverse can be added to the icon with the fa-stack-1x to help with a knock-out looking effect.

Does Font Awesome use SVG?

Just like any other style of Font Awesome icons, you can drag and drop a Duotone SVG file into your SVG-friendly application, and the duotone icon will appear.

What is fa-stack-2x?

fa-stack-2x. Used on the icon which should be displayed larger when stacked. fa-inverse. Inverts the color of the icon displayed at base size when stacked.

Can I use SVG with fontawesome?

FontAwesome offers native SVG support in their latest release. So this article is only relevant if you use the old FontAwesome v4. 1. Create a SVG sprite. With Font Icons you have a font with all icons included in one file and choose which icon we want to display within the code.

How to display all icons in one file using fontawesome?

So this article is only relevant if you use the old FontAwesome v4. 1. Create a SVG sprite With Font Icons you have a font with all icons included in one file and choose which icon we want to display within the code. We want to have the same behavior with SVG icons so we need to create a sprite that has all SVG icons in one file.

Is there a way to toggle display SVG icons in webfont?

Previously in the old WebFont method, this was achieved by toggling or changing the class on the tag, however as these are now rendered as SVG's in the source code this no longer works. Is there a way to do this without needing to render both SVG icons in source code and using additional classes/CSS to toggle display?

How to display all SVG icons in one file?

With Font Icons you have a font with all icons included in one file and choose which icon we want to display within the code. We want to have the same behavior with SVG icons so we need to create a sprite that has all SVG icons in one file. Font Awesome doesn't offer such a download so a neat way of doing this is to use IcoMoon.


3 Answers

Font Awesome 5.0.0 has just been released and the migration from 4.7 to 5.0 wrecked up my javascript/jquery to change a "fa-star-o" icon to "fa-star" when the user clicks on it.

I managed to fix it so I wanted to share with you these two tips:

The icon in HTML:

<i class="foo fas fa-star"></i>

1) Change icon with jQuery (from "star" to "alarm-clock" and vice versa):

var icon = $('.foo');
var icon_fa_icon = icon.attr('data-icon');

if (icon_fa_icon === "alarm-clock") {
    icon.attr('data-icon', 'star');
} else {
    icon.attr('data-icon', 'alarm-clock');
}

2) Change icon-style with jQuery (from 'fas' to 'far'):

var icon = $('.foo');
var icon_fa_prefix = icon.attr('data-prefix');

if (icon_fa_prefix === 'fas') {
    icon.attr('data-prefix', 'far');

} else {
    icon.attr('data-prefix', 'fas');
}

Hope that helps anyone with the same issue.

like image 158
Pascal Avatar answered Oct 16 '22 09:10

Pascal


Verified with FA 5.0.2

I modified the original documentation found on Font-Awesome's website here. The selector on their website wasn't selecting the proper element, so we need to modify the attribute.

HTML

<div class='icon'><i class='far fa-minus-square'></i></div>

The class of the div doesn't really matter so much as we can change it. Looking at the javascript, we are using the element to find the svg and specifically, we are looking for the data-icon attribute. Once we know the data attribute, we can change it every time it is clicked.

So in this case, it starts off with the minus-square. If the icon is the minus-square, it changes it to the plus-square. If it is not the plus-square, it will change it to the minus-square.

JQuery

  document.addEventListener('DOMContentLoaded', function () {
    $('.icon').on('click', function () {
      if ($(this).find('svg').attr('data-icon') == 'minus-square' ) {
        $(this).find('svg').attr('data-icon', 'plus-square');
      } else {
        $(this).find('svg').attr('data-icon', 'minus-square');
      };
    });
  });
like image 13
Steven Avatar answered Oct 16 '22 07:10

Steven


Assuming you are using the recommended method with the fontawesome.js sheet, I found this on the official documentation:

Changing icons by changing class:

<button>Open up <i class="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>
like image 8
nicozica Avatar answered Oct 16 '22 09:10

nicozica