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
4 Answers
Reset to default 4HTML:
<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
版权声明:本文标题:javascript - JQuery Adding the values of 2 divs into a third div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744388696a2603873.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论