Elitmiar
Elitmiar

Reputation: 36829

How to calculate the total value of input boxes using jquery

I have a list of input boxes, now I need to calculate the total of all values entered in input boxes with the following naming convention pre[0],pre[1],pre[2] etc.

Is this possible with Jquery and how?

Upvotes: 2

Views: 5652

Answers (2)

NimChimpsky
NimChimpsky

Reputation: 47280

I would do it like this

var sum = 0;

find("input[name*='pre']").each(function(index) {

sum = sum + this.val();

 })

Upvotes: 0

Yi Jiang
Yi Jiang

Reputation: 50095

Would something like this work?

var sum = 0;

$('input[name^="pre"]').each(function(){
    sum += parseFloat(this.value);
});

^= is the Attribute Starts With Selector.

Upvotes: 10

Related Questions