I have the number $likes_number
in a div block on my page. I would like to know how to increment it dynamically by clicking on a button?
My code:
<div id="likes">
<span class="figure">
<?php echo $likes_number ;?>
</span>
</div>
<button type="button" id="like" >Like</button>
What is the code if you want to increment the count button increment? // Select increment and decrement buttons const incrementCount = document. getElementById(“increment-count”); const decrementCount = document. getElementById(“decrement-count”); // Add click event to buttons incrementCount.
The stepUp() method increments the value of the number field by a specified number. Tip: To decrement the value, use the stepDown() method.
JavaScript has an even more succinct syntax to increment a number by 1. The increment operator ( ++ ) increments its operand by 1 ; that is, it adds 1 to the existing value. There's a corresponding decrement operator ( -- ) that decrements a variable's value by 1 . That is, it subtracts 1 from the value.
On clicking Increment (+), user will be able to increment the number in input type number. On clicking Decrement (-), user will be able to decrement the number in input type number.
<script type="text/javascript" src="//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>
Live example: https://jsfiddle.net/nTmu7/2/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With