I have rows of input boxes (text) that I need to iterate over, multiplying values within a row and then summing the products. The only solution I could find is to convert the input boxes to arrays:
var array1 = $('input[id$="txtVal1"]').toArray();
var array2 = $('input[id$="txtVal2"]').toArray();
var temp1;
var temp2;
var sum=0;
And then iterate and sum using:
for (i = 0; i < array1.length; i++) {
if (array1[i].value.length > 0) { //make sure we have data
temp1 = parseFloat(array1[i].value);
temp2 = parseFloat(array2[i].value);
sum += temp1 * temp2;
}
}
This works. However I'm just learning JQuery and want to use the canonical method.
you can loop directly over all the items found through a selector like this:
$('input').each(function(index,data) {
var value = $(this).val();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With