Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment a number by clicking onto a button

Tags:

jquery

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>
like image 624
Anon Avatar asked Aug 24 '11 22:08

Anon


People also ask

What is the code if you want to increment the count button increment?

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.

How do you increment numbers in HTML?

The stepUp() method increments the value of the number field by a specified number. Tip: To decrement the value, use the stepDown() method.

How do you increment a number in JavaScript?

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.

What is increment button?

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.


1 Answers

<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/

like image 161
Pedro Lobito Avatar answered Sep 29 '22 09:09

Pedro Lobito