Using jQuery to count all inputs fields value

I have developed this code to get sum of multiple input field’s value by using jQuery.
HTML and PHP :-

<?php 
    if(!empty($businessSerList)) 
    { 
        foreach ($businessSerList as $key = $busiSerList) 
        { 
?>
            <input id="percentagePopUp<?php echo $key; ?>" class="form-control parsley-validated BUSLinput" name="percentagePopUp[]" type="text" value="<?php if(in_array ($busiSerList->business_service_area_id, $result)) { echo $perse[$busiSerList->business_service_area_id]; } ?>" data-type="number" >
<?php 
        } 
    } 
?>


jQuery:-

var sum=0;
$(".BUSLinput").each(function(){
    if($(this).val() != "")
    sum += parseInt($(this).val());
});

if(sum >= 101)
{
    alert('Error: More than 100% allocated.');
    $("#"+fieldID).val("");
    return false;
}
else
{
    alert('alert value less then 100%');
}

 

Leave a Reply