Increase the number by pressing the button

I have the number $likes_number in the div block on my page. I would like to know how to increase it dynamically by pressing a button?

My code is:

 <div id="likes"> <span class="figure"> <?php echo $likes_number ;?> </span> </div> <button type="button" id="like" >Like</button> 
+6
source share
2 answers
 <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.js"></script> <div id="likes"> <span class="figure"></span> </div> <button type="button" id="like">Like</button> <script type="text/javascript"> var clicks = 0; $("#like").click(function(){ clicks++; $('.figure').html(clicks);}); </script> 

Real-time example: http://jsfiddle.net/nTmu7/2/

+6
source
 $('#like').click(function() { $('#likes span').text( parseInt($('#likes span').text()) + 1 ); }); 

Here you can see an example: http://jsfiddle.net/nayish/GCvnH/

+3
source

Source: https://habr.com/ru/post/895813/


All Articles