javascript - JQuery Adding the values of 2 divs into a third div - Stack Overflow

I have 3 divs with numbers in each...<div id="one">1<div><div id="two&qu

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()));
});​

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744388697a4571798.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信