
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to toggle between a like/dislike button with CSS and JavaScript?
To toggle between like/dislike button with CSS and JavaScript, the code is as follows −
Example
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"/> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; padding: 20px; } .fa { font-size: 50px; cursor: pointer; user-select: none; } .fa-thumbs-up { color: blue; } .fa-thumbs-down { color: red; } .fa:hover { transform: scale(1.2); } </style> </head> <body> <h1>Like dislike button example</h1> <i class="fa fa-thumbs-up"></i> <script> document.querySelector(".fa").addEventListener("click", function(event) { toggleLike(event); }); function toggleLike(ele) { ele.target.classList.toggle("fa-thumbs-down"); } </script> </body> </html>
Output
The above code will produce the following output −
On clicking the like button −
- Related Questions & Answers
- How to create a "toggle switch" (on/off button) with CSS?
- How to toggle between password visibility with JavaScript?
- How to create a Tkinter toggle button?
- How to toggle between hiding and showing an element with JavaScript?
- How to create a Toggle Button in JavaFX?
- How to toggle text with JavaScript?
- How to toggle between adding and removing a class name from an element with JavaScript?
- How to create a search button with CSS?
- How to center a button element vertically and horizontally with CSS?
- Create a button look like a link with Bootstrap
- How to toggle a boolean using JavaScript?
- With CSS add transparency to a button
- How to create a split button dropdown with CSS?
- How to add rounded corners to a button with CSS?
- How to add a button to an image with CSS?
Advertisements