S
S
Sergey Petrov2019-02-21 18:22:43
AJAX
Sergey Petrov, 2019-02-21 18:22:43

How to check for a repeat click?

there is a code, when clicked, the value changes

echo '<div class="like">';
          if(какое то условие ){
              echo '<div class="Like_act" data-id="'.$posting['id'].'">5</div>';	   
       				}else{
              echo '<div class="Like_bck" data-id="'.$posting['id'].'">5</div>';		   				
       				}
echo '</div>';

$(document).ready(function() {
    $(".like").bind("click", function(event) {
    	var linc = $(this);
   	var dit = $(this).attr("data-id");    
      $.ajax({
        url: "ajaxLike.php",
        type: "POST",
        data: ("id=" +dit),
        dataType: "text",
        success: function(result) {
        	linc.addClass('Like_act');
         linc.text(Number(linc.text()) + 1);
        }
        
      });
    });
  });

changes the value well, I can’t figure out how to make it so that when you click again, the value is also sent, but it changes to the original value, that is, it is 1 and the replacement linc.addClass('Like_act');

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FeST1Val, 2019-02-21
@zubrrap

Why not just do

<div class="like__box">
  <div class="like"></div>
</div>

in php initially put the class you need, then just check the class
$('.like').click(function() {
  if($(this).hasClass('Like_act')) {
    //... тут ваши Ajax запросы)
  } else {
    //... тут ваши Ajax запросы)
  }
})

V
Vitsliputsli, 2019-02-21
@Vitsliputsli

Do you have +1 for like_act and -1 for like_bck? Here also check presence/absence of a class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question