Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Toggle Class With Font Awesome Not Working

I have multiple buttons, when a like button is clicked I would like the font awesome icon to switch as well, however, the way I have it set up the font awesome icon just disappears, but I can see the class change. Not sure where to go from here.

$("button").click(function() {
  $('#like' + this.id).toggleClass("fa-regular fa-thumbs-up");
});
<script src="https://kit.fontawesome.com/317f6467e2.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button class="like-button btn" id="1">
      <i class="fa-solid fa-thumbs-up" id="like1"></i>
    </button>

<button class="like-button btn" id="2">
      <i class="fa-solid fa-thumbs-up" id="like2"></i>
    </button>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If you want to change icon/style just add other class to toggleClass like:

$("button").click(function() {
    $('#like'+this.id).toggleClass("fa-solid fa-thumbs-up fa-regular fa-thumbs-up");
  });
<script src="https://kit.fontawesome.com/317f6467e2.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<button class="like-button btn" id="1">
  <i class="fa-solid fa-thumbs-up" id="like1"></i>
</button>

<button class="like-button btn" id="2">
  <i class="fa-solid fa-thumbs-up" id="like2"></i>
</button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading