简体   繁体   中英

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM