Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  102] [ 6]  / answers: 1 / hits: 23375  / 12 Years ago, tue, march 6, 2012, 12:00:00

I have a list of buttons with numeric values and a total number displayed on top of the page.



OnClicking one of the buttons (e.g. Add 100), I want the integer value of 100 to be added to the total points displayed. I want the total to update instantly versus the page having to refresh every time.



Do I have the right idea? Is this possible with JavaScript and jQuery or do I need to try something else?


More From » jquery

 Answers
71

Using jquery:



http://jsfiddle.net/mYuRK/



HTML



<button value=100>100</button>
<button value=200>200</button>
<button value=300>300</button>
<div class=total></div>


JS



var theTotal = 0;
$('button').click(function(){
theTotal = Number(theTotal) + Number($(this).val());
$('.total').text(Total: +theTotal);
});

$('.total').text(Total: +theTotal);

[#87025] Monday, March 5, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
benitoh

Total Points: 150
Total Questions: 113
Total Answers: 104

Location: India
Member since Wed, Aug 26, 2020
4 Years ago
benitoh questions
Sun, Mar 21, 21, 00:00, 3 Years ago
Mon, May 13, 19, 00:00, 5 Years ago
;