0

when i click on like button then it change the class and inner html , but when i click on a unlike button then it is not working. how do i solve this problem?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jquery</title>

<!-- jquery file -->
<script src="jquery.js" type="text/javascript"></script>

 <!-- bootstrap files  -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css" type="text/css" />

<script type="text/javascript">
$(function(){
    $(".like").click(function(){
        $(this).html("Unlike");
        $(this).attr("class","btn btn-success unlike");

    })
    $(".unlike").click(function(){
        $(this).html("Like");
        $(this).attr("class","btn btn-primary like");
    })

})
</script>
</head>
<body>
    <button class='btn btn-primary like'>LIKE</button>
</body>
</html>
  • You're changing the `class` attributes at runtime, therefore you need to use a delegated event handler – Rory McCrossan Aug 10 '17 at 09:02
  • Actually you are probably better to have just one click event and then check the state of the button to determine what action to take. toggle effect – musefan Aug 10 '17 at 09:03
  • what is the error you get in console? – Anil Yadav Aug 10 '17 at 09:04
  • i have changed the class attribute from like to unlike –  Aug 10 '17 at 09:07
  • there is no error in console but when i click on unlike button which is class then it is not change the unlike class into like class –  Aug 10 '17 at 09:08
  • Rory McCrossan how it is duplicate and please check my code ... –  Aug 10 '17 at 09:20
  • $(function(){ $(".like").click(function(){ $(this).html("Unlike"); $(this).removClass("btn btn-primary like"); $(this).addClass("btn btn-success unlike"); }) $(".unlike").click(function(){ $(this).html("Unlike"); $(this).removClass("btn btn-success unlike"); $(this).addClass("btn btn-primary like"); }) }) –  Aug 10 '17 at 09:39

0 Answers0