Sum of values in input fields grouped by class attribute using jQeury

Here is the jQuery way to calculate the sum of all values stored in input fields grouped by class “.subtotal”. In this example the sum will be shown as an alert as soon as you leave a field after entering some numeric value in it (captured as an onblur event).

$('.subtotal').blur(function () {
var sum = 0;
$('.price').each(function() {
sum += Number($(this).val());

alert(sum); //it will alert sum when you leave  a field (captured as an onblur event).

Leave a Reply

Your email address will not be published. Required fields are marked *