Using font-awesome and css, is it possible for a css change to alter the icon?

StackOverflowed

Assume I have the following css:

<style>
  .liked { 
      color: #600;
   }

   .not-liked {
       color: #aaa;
   }
</style>

Assuming I have this: <i class = 'fa fa-heart-o not-liked' id = "banner"></i>

in my jQuery, if I swap classes from not-liked to liked can I also swap fa-heart-o to fa-heart in the same class?

So I'm trying to avoid this:

function() {
   $("#banner").removeClass('not-liked');
   $("#banner").removeClass('fa-heart-o');
   $("#banner").addClass('liked');
   $("#banner").addClass('fa-heart');
}

and basically switch the colour and the icon with this:

function() {
   $("#banner").removeClass('not-liked');
   $("#banner").addClass('liked');
}
Oka

FontAwesome glyphicons can be accessed via their Unicode identifiers. In fact, all the predefined classes provided by the stylesheet are created in the same way, using the ::before pseudo-element selector, and content.

We can see fa-heart is f004, and fa-heart-o is f08a.

If you want to reduce the class list overhead, for both your HTML and JavaScript, just adjust your own classes to represent exactly what you need.

$('.toggle').on('click', function () {
  $(this).toggleClass('not-liked liked');
});

$('.once').on('click', function () {
  $(this).removeClass('not-liked').addClass('liked');
});
.liked::before {
  color: #600;
  content: '\f004';
}

.not-liked::before {
  color: #aaa;
  content: '\f08a';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">

<span class='toggle fa not-liked'></span>
<span class='once fa not-liked'></span>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using font-awesome and css, is it possible for a css change to alter the icon?

From Dev

Changing Font Awesome icon using CSS does not work

From Dev

change shape of font icon using css

From Java

Use Font Awesome icon as CSS content

From Dev

CSS Awesome Font icon as submit button

From Dev

Remove padding below font awesome (CSS icon)

From Dev

Using icon font in css content

From Dev

Draw a slash line over glyphicon or font-awesome icon using CSS

From Dev

How to set equal amount of spacing from the font awesome icon or text using css

From Dev

Using Font Awesome in Kendo Grid buttons with CSS

From Dev

CSS3 rotation when hover a font awesome icon?

From Dev

Replace <img> tag with font awesome icon from CSS

From Dev

CSS3 transition within Awesome Icon Font and Text Hover

From Dev

How to input block of text next to Font Awesome icon in CSS?

From Dev

How to change button text to font awesome icon using jquery

From Dev

How to change button text to font awesome icon using jquery

From Dev

Font Awesome i css

From Dev

Custom Font Awesome CSS

From Dev

Custom Font Awesome CSS

From Dev

modify the css for font awesome

From Dev

Using Font Awesome as Select Icon

From Dev

Using Font Awesome as Select Icon

From Dev

Changing Font Icon in WPF using Font Awesome

From Dev

Changing Font Icon in WPF using Font Awesome

From Dev

Change color when hover a font awesome icon?

From Dev

change font-awesome icon when toggled

From Dev

How to change font awesome icon hover color?

From Dev

change font-awesome icon when toggled

From Dev

Change font awesome icon on click menu bars