How to toggle between a like/dislike button with CSS and JavaScript?

JavascriptWeb DevelopmentObject Oriented Programming

To toggle between like/dislike button with CSS and JavaScript, the code is as follows −

Example

 Live Demo

<!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 −

raja
Published on 08-May-2020 13:19:46
Advertisements