Wednesday, July 3, 2013

calculate total value of checkboxes using jquery

If one checkbox is selected, it will add the values of checkboxes automatically. If you will click unchecked, the total will be updated automatically.
JQUERY
$(document).ready(function() {
$("input[type=checkbox]").change(function(){
  recalculate();
});
function recalculate(){
    var sum = 0;
    $("input[type=checkbox]:checked").each(function(){
      sum += parseInt($(this).attr("rel"));
    });
  //  alert(sum);
$("#output").html(sum);
}
});
HTML
<div id="school">
    <p><input type="checkbox" rel="15">Book</p>
    <p><input type="checkbox" rel="15">Bag</p>
    <p><input type="checkbox" rel="15">Notebook</p>
</div>

<span id="output"></span>

Ref: http://stackoverflow.com/questions/5066537/calculate-total-value-checkboxes-jquery

No comments:

Post a Comment