How to sum value of checked changed checkbox jquery?
http://stackoverflow.com/a/32022458/1144203
HTML
<form method="post">
<fieldset>
<legend>Menu</legend>
<input type="checkbox" id="checkall" />Check All<br />
<input type="checkbox" name="opt" id="candy" value="2" />Candy<br />
<input type="checkbox" name="opt" id="food" value="€ 3" />Food<br />
<input type="checkbox" name="opt" id="water" value="6.99" />Water<br />
</fieldset>
</form>
JavaScript
(function($){
$("#checkall").change(function() {
toggleCheckAll(this.checked)
totalCount = calculateAll()
alert(totalCount);
});
$("input[name='opt']").change(function() {
totalCount = calculateAll()
alert(totalCount);
});
function toggleCheckAll(checked){
$("input[name='opt']").prop('checked', checked)
}
function calculateAll(){
count = 0;
$("input[name='opt']").each(function(index, checkbox){
if(checkbox.checked)
count += parseInt(checkbox.value)
})
return count;
}
})(jQuery);