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?
Would something like this work?
var sum = 0;
$('input[name^="pre"]').each(function(){
sum += parseFloat(this.value);
});
^= is the Attribute Starts With Selector.
I would do it like this
var sum = 0;
find("input[name*='pre']").each(function(index) {
sum = sum + this.val();
})
this.value or $(this).val()this.value is better because it does not create an additional jQuery object.
blurevent, while this one requires that selector. You can conceivably merge them, but keep doing this to subtlety different questions and we'll end up with the jQuery documentation page.