tags:

views:

81

answers:

2

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?

+6  A: 

Would something like this work?

var sum = 0;

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

^= is the Attribute Starts With Selector.

Yi Jiang
Ah, yours is better than mine.
NimChimpsky
A: 

I would do it like this

var sum = 0;

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

sum = sum + this.val();

 })
NimChimpsky
it's `this.value` or `$(this).val()`
Reigel
`this.value` is better because it does not create an additional jQuery object.
Yi Jiang