Javascript coders help: accessing fields using a loop

TheSomeone

Newbie
Joined
Dec 24, 2004
Messages
2,186
Reaction score
0
Knowing how many there are how can i access all the fields like this:
document.myForm.var1.value
document.myForm.var2.value
etc...
How can I do this using a for loop? Is it even possible? basically, can I replace "var1" or "var2" by a string which changes every iteration of the loop?

Thanks for your help.
 
In case anyone is interested:

Code:
var count = 10;
var x, ele = document.myForm.elements;
for(x=1; x<=count; ++x) {
    alert(ele["var"+x].name + ' = "' + ele["var"+x].value + '"');
}
 
Hey, if you don't mind, explain what you are doing here - specifically why you have elements named var1, var2, etc. There's probably a better way to do it.
 
it would help if it was in context, but if you wanted to access all of the values in tandem it may require a rewrite by putting all of those individual values into an array. Otherwise there's basically no way to compress those variables into algorithmic form without physically calling each variable. My suggestion is to just copy paste if there's less than 20 elements. If there are more, you'll definatley need to rewrite the "myForm" class and put all of the vars in an array.
 
Back
Top