admin管理员组

文章数量:1403500

I have 3 divs with numbers in each...

<div id="one">1</div>

<div id="two">5</div>

<div id="total">0</div>

What I need to do for example is:

If #one is click then Add the values of #one and #two and update it on #total

So, in the case above total would look like this:

<div id="total">6</div>

I have 3 divs with numbers in each...

<div id="one">1</div>

<div id="two">5</div>

<div id="total">0</div>

What I need to do for example is:

If #one is click then Add the values of #one and #two and update it on #total

So, in the case above total would look like this:

<div id="total">6</div>
Share Improve this question asked Aug 15, 2012 at 15:43 Satch3000Satch3000 49.5k90 gold badges225 silver badges349 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 4

HTML:

<div id="one">1</div>

<div id="two">5</div>

<div id="total">0</div>

<input id="btn-calculate" type="button" value="Calculate" />

JavaScript:

var one = document.getElementById('one'),
    two = document.getElementById('two'),
    total = document.getElementById('total');
document.getElementById('btn-calculate').onclick = function() {
    total.innerHTML = parseInt(one.innerHTML) + parseInt(two.innerHTML);
};

Demo

$('#one').click(function(){
$("#total").text(
    parseFloat($(this).text()) + 
    parseFloat($("#two").text())
);
});
​$("#one")​.click(function(){
    $("#total").html(parseInt($(this).text()) + parseInt($("#two").text()))
})​

http://jsfiddle/daniilr/G4Snm/

Try this,

Live Demo

$("#one").click(function() {
   $('#total').text(parseFloat($('#one').text()) + parseFloat($('#two').text()));
});​

本文标签: javascriptJQuery Adding the values of 2 divs into a third divStack Overflow