Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery calculate sum of values in all text fields

Tags:

jquery

I have an order form with about 30 text fields that contain numerical values. I'd like to calculate the sum of all those values on blur.

I know how to select all text fields but not how to loop through them and add up all their values?

$(document).ready(function(){    $(".price").blur(function() {     //loop and add up every value from $(".price").val()    }) }); 
like image 527
stef Avatar asked Mar 10 '10 14:03

stef


People also ask

How do you calculate total dynamically on text change using JQuery?

cell'+i,function(){ var sum1 = 0; $(". cell"+i). each(function() { var get_val = $(this). val(); sum1 += parseFloat(get_val); }); $("#total_sum_value1").


1 Answers

$('.price').blur(function () {     var sum = 0;     $('.price').each(function() {         sum += Number($(this).val());     });      // here, you have your sum });​​​​​​​​​ 
like image 84
Marko Dumic Avatar answered Sep 23 '22 08:09

Marko Dumic