Home > Blockchain >  Toggle Class With Font Awesome Not Working
Toggle Class With Font Awesome Not Working

Time:07-02

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  id="1">
      <i  id="like1"></i>
    </button>

<button  id="2">
      <i  id="like2"></i>
    </button>

CodePudding user response:

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  id="1">
  <i  id="like1"></i>
</button>

<button  id="2">
  <i  id="like2"></i>
</button>

  • Related