1

I am new here

I have this code

<div class="like-buttons">
    <div class="liked"></div><button class="dislike like"><span class="countl">12</span></button>
</div>

https://codepen.io/Void0000/pen/oNzGmGr

and I need to make (with jQuery) some function, that when I will click on my button, it will be count +1 (eg: if it was 2 it becomes 3), and when I click again on it, it will count -1 (eg: if it was 3 it now becomes 2) and etc.

Void0000
  • 13
  • 4

4 Answers4

2

According to @Shivam 's answer you can change like this;

$('.like').onclick(function(){
     let like = $(this).closest('.countd').html();
     if($(this).hasClass("clicked")){
         $(this).removeClass("clicked");
         like++;
     }
     else{
         $(this).addClass("clicked");
         like--;
     }
     $(this).closest('.countd').html(like)
});

Can you check this Link. I made some changes.

$(document).ready(function() {
   $('.like').click(function() {
       let like = parseInt($('.countl').html());
       if ($(this).hasClass("clicked")) {
           $(this).removeClass("clicked");
           like++;
       } else {
           $(this).addClass("clicked");
           like--;
       }
       $('.countl').html(like)
   });
});
cbalakus
  • 620
  • 7
  • 15
1

Set event listeners on the classes, by jQuery. Like -

$('.like').onclick(function(){
     let like = $(this). closest('.countd').html();
     like = like + 1;
});

Do same for dislike button.

Shivam Shukla
  • 73
  • 1
  • 11
  • I am sorry, i wrote that i have two buttnos, it mistake, a have only 1 button, so i need this fucntion only for one button. So i clack once on button +1, i click once again on this button -1, etc< only 1 button – Void0000 Dec 24 '20 at 04:31
0

I am sorry, i wrote that i have two buttnos, it mistake, a have only 1 button, so i need this fucntion only for one button. So i clack once on button +1, i click once again on this button -1, etc< only 1 button!

Void0000
  • 13
  • 4
  • According to #Shivam 's answer, you can add "clicked" class to your button if it hasn't that class. If it has that class you need to remove class from it. While you are checking it has class or not you can increase your like or discrese. – cbalakus Dec 24 '20 at 07:43
0

Unfortunetly, its all not working = (

Void0000
  • 13
  • 4
  • Can you check my answer again? I updated and tested it. There were some errors before. I didn't check it well. – cbalakus Dec 25 '20 at 14:35