stef stef - 1 year ago 88
jQuery Question

jQuery calculate sum of values in all text fields

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?

$(".price").blur(function() {
//loop and add up every value from $(".price").val()

Answer Source

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

    // here, you have your sum