PDA

View Full Version : iterating through form elements and their values


cepheus1122
03-26-2001, 01:02 PM
Hi. I have a cgi script that generates a number of textfields for inputing names based on a number given by the client. If the client enters 10 on the form the script generates a page with 10 filefields named name1,name2,...name10. I want to iterate through the names, get the values and do some validation. I cannot figure out how to do the iteration though. I am able to iterate through the names using a for in loop on the form, but cannot get the values. Any help would be appreciated. Thanks.

-jim

kdjoergensen
03-27-2001, 02:41 PM
var valueTotal = 0;
for (var j=0;j<form.elements.length;j++){
valueTotal += parseFloat(form.elements[j].value);
}

alert(valueTotal);

I assume you wanted to add up values in the fields (e.g. numeric input which is being added up).

Simply subsitute 'form' with the name of your form.
Kenneth

[Edited by kdjoergensen on 03-27-2001 at 02:47 PM]