Abdallah Abu Hazeem Abdallah Abu Hazeem - 1 month ago 9
Ajax Question

pass the sum of checked checkboxes to html text input

hi every body im new in coding ,here im trying to evaluate the sum of of values of checked checkboxes and pass the sum to html text input i dont no how i appreciate
help , this is my code:



Aroma Massage:<input type="checkbox" name="Aroma massage" id="check1" value="55">
Swedish Massage:<input type="checkbox" name="Swedish massage" id="check2" value="50">
Hot Stone Massage:<input type="checkbox" name="Hot Stone massage" id="check3" value="70">
Stress Recovery Massage:<input type="checkbox" name="Stress recovery massage" id="check4" value="25">
Thai Massage:<input type="checkbox" name="Thai massage" id="check5" value="60">

<div id="tot"></div>




Answer

$(document).ready(function () {
    // cache the inputs and bind the events
    var $inputs = $('input[type="checkbox"]')
    $inputs.on('change', function () {
        var sum = 0;
        $inputs.each(function() {
        // iterate and add it to sum only if checked
           if(this.checked)
               sum += parseInt(this.value);
        });
        $("#price").val(sum);
    });
});