...

View Full Version : Loop through form elements



rswyatt
01-23-2006, 09:19 PM
Hello all.

I've read post after post with no apparent relief. :-(

What I have is 8 select boxes. They are named zoneX (with X being 1-8)..

I have a for loop that I would like to create to set the value of each on an event.

This is the for loop that I've created:



var eachZone = curLayout.split(";");
errbg = '#AF0050';
normbg = '#6B6883';

for(i=0; i < 9; i++){
var iplus = i + 1;

if(eachZone[i] > thisNum && eachZone[i] != "text"){
document.editForm.elements['zone' + iplus].value = eachZone[i];
document.editForm.elements['zone' + iplus].style.backgroundColor = normbg;
}
else {
document.editForm.elements['zone' + iplus].style.backgroundColor = errbg;
}


}



I am setting the value of the array 'eachZone' earlier in the script so I didn't bother including it here. Know that it is 8 keys long and each key contains either a number (1-5) or the word "text"...

Anyhow - everytime I run this I get an error "document.editForm.elements[...].style is null or not an object"...

Any ideas? Am I going about this all wrong?

arnyinc
01-23-2006, 10:58 PM
Second edit: It looks like the "i" variable increments up to 8. Then you set iplus=i+1. So you are looking for a zone9 which doesn't exist.

A couple notes:
I'm not sure of the order of operation for:
if(eachZone[i] > thisNum && eachZone[i] != "text")
You might need another two sets of parens:
if((eachZone[i] > thisNum) && (eachZone[i] != "text"))

I just noticed they are select boxes. You can't directly access the "value" of a select box. You have to look up the value of the selectedIndex:
document.editForm.elements['zone' + iplus].value = eachZone[i];

glenngv
01-24-2006, 03:50 AM
A couple notes:
I'm not sure of the order of operation for:
if(eachZone > thisNum && eachZone[i] != "text")
You might need another two sets of parens:
if((eachZone[i] > thisNum) && (eachZone[i] != "text"))
Parens are not required though good to have for readability.



I just noticed they are select boxes. You can't directly access the "value" of a select box. You have to look up the value of the selectedIndex:
document.editForm.elements['zone' + iplus].value = eachZone[i];You can do [I]select.value in modern browsers but not in older browsers (I only know NS4 which doesn't support this).

arnyinc
01-24-2006, 05:14 AM
Oops, I was overanalyzing the code. Maybe I'll keep my little "notes" to myself from now on.

I actually learned something about using value in a select box though! :D

rswyatt
01-24-2006, 02:16 PM
Oh my... Talk about the "dumb" question of the year. Do I get a prize for that?

I totally looked over the fact that I was calling for a zone9 when in fact there is no such element.

Thanks guys for pointing out the obvious. :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum