Calculate the total cost value of jquery

http://jsfiddle.net/kcd6r/

How to calculate the total rel value? if one checkbox is selected, the total will be automatically updated.

+5
source share
2 answers

Hope this helps:

$("input[type=checkbox]").change(function(){
  recalculate();
});


function recalculate(){
    var sum = 0;

    $("input[type=checkbox]:checked").each(function(){
      sum += parseInt($(this).attr("rel"));
    });

    alert(sum);
}
+13
source

Same thing with JS:                               

    <script type="text/javascript">

    var total = 0;

    function test(item){
        if(item.checked){
           total+= parseInt(item.value);
        }else{
           total-= parseInt(item.value);
        }
        //alert(total);
        document.getElementById('Totalcost').innerHTML = total + " /-";
    }



    </script>
    </head>
    <body>

    <div id="container">
    <input type="checkbox" name="channelcost" value="10" onClick="test(this);"  />10<br />
    <input type="checkbox" name="channelcost" value="20" onClick="test(this);" />20 <br />
    <input type="checkbox" name="channelcost" value="40" onClick="test(this);" />40 <br />
    <input type="checkbox" name="channelcost" value="60" onClick="test(this);" />60 <br />
    </div>
    Total Amount : <span id="Totalcost"> </span>
    </body>
    </html>
+3
source

All Articles