Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Location
    Des Moines, Iowa
    Posts
    219
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Loop through form elements

    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:

    Code:
    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?
    Rich S. Wyatt
    D3 Web Creations

  • #2
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    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];
    Last edited by arnyinc; 01-23-2006 at 10:06 PM.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,031
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Quote Originally Posted by arnyinc
    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"))
    Parens are not required though good to have for readability.

    Quote Originally Posted by arnyinc
    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 select.value in modern browsers but not in older browsers (I only know NS4 which doesn't support this).

  • #4
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    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!

  • #5
    Regular Coder
    Join Date
    Jan 2004
    Location
    Des Moines, Iowa
    Posts
    219
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Rich S. Wyatt
    D3 Web Creations


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •