Caluculate Total on checkbox checked in javascript/jquery

Caluculate Total on checkbox checked in javascript/jquery

HTML

<div id="rslist">
    <input type="checkbox" id="cat_1" value="cat_1" rs="1.5" /><label for="cat_1">cat_1</label><br/>
    <input type="checkbox" id="cat_2" value="cat_2" rs="2" /><label for="cat_2">cat_2</label><br/>
    <input type="checkbox" id="cat_3" value="cat_3" rs="3.5" /><label for="cat_3">cat_3</label><br/>
    <input type="checkbox" id="cat_4" value="cat_4" rs="4" /><label for="cat_4">cat_4</label><br/>
    <input type="checkbox" id="cat_5" value="cat_5" rs="5" /><label for="cat_5">cat_5</label><br/>
    <input type="checkbox" id="cat_6" value="cat_6" rs="6.5" /><label for="cat_6">cat_6</label><br/>
    <input type="checkbox" id="cat_7" value="cat_7" rs="7" /><label for="cat_7">cat_7</label><br/>
    <input type="checkbox" id="cat_8" value="cat_8" rs="8" /><label for="cat_8">cat_8</label><br/>
    <input type="checkbox" id="cat_9" value="cat_9" rs="9.5" checked="checked" /><label for="cat_9">cat_9</label>
</div>
<input type="text" id="totalsum" value="0" readonly="readonly" />

Javascript

function allTotalvalue(){
    var totalsum = 0;
    $('#rslist :checkbox[checked]').each(function(){
        totalsum += parseFloat($(this).attr('rs')) || 0;
    });
    $('#totalsum').val(totalsum);
}

$('#rslist :checkbox').click(function(){
    allTotalvalue();
});
allTotalvalue();

CSS

label{margin-left:0.5em;}

Calculate Sum of Input Fields = Total

Calculate Sum of Input Fields = Total

HTML

<div>
     <h2>List sum</h2>
    
     <p><strong>Book</strong><br />
         $ <input onblur="findTotal()" type="text" class="price" name="printcost" /></p>
    
     <p><strong>pancil</strong><br />
         $ <input onblur="findTotal()" type="text" class="price" name="digitalcost" /></p>
    
     <p><strong>bag</strong><br />
         $ <input onblur="findTotal()" type="text" class="price" name="dmscost" /></p>
    
     <p><strong>dress</strong><br />
         $ <input onblur="findTotal()" type="text" class="price" name="preprintcost" /></p>
    
     <p><strong>Total sum <span class="required">*</span></strong><br />
         $ <input type="text" name="totalorder" id="totalorder" /></p>
</div>

Javascript

function findTotal(){
    var arr = document.getElementsByClassName('price');
    var totalvalue=0;
    for(var i=0;i<arr.length;i++){
        if(parseFloat(arr[i].value))
            totalvalue += parseFloat(arr[i].value);
    }
    document.getElementById('totalorder').value = totalvalue;
}

Kategori

Kategori