Welcome to WebmasterWorld Guest from 23.22.182.29

Forum Moderators: open

Message Too Old, No Replies

document.getElementById proofread help

what am I missing?

     
8:22 pm on Dec 8, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 26, 2006
posts: 41
votes: 0


Hi,

I'm stumped as to why this simple javascript won't rewrite a select menu's options to set the first one to "selected".

var theElements = new Array();
theElements[0] = "filter_request_3";
theElements[1] = "filter_request_4";
theElements[2] = "filter_request_5";
theElements[3] = "filter_request_6";
theElements[4] = "local_base";

for (i=0;i<theElements.length;i++) {
document.getElementById(theElements[i]).options[0].selected = true;
}

Is there a syntax thing I'm missing regarding the getElementById() portion?

FWIW, the for loop works great, and traverses the array just fine. It's just the select menu option isn't ever changed.

Any ideas appreciated!

Glenn

8:46 pm on Dec 8, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


Syntax?
document.getElementById(theElements[i])[0].selected = true;
9:46 pm on Dec 8, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 26, 2006
posts: 41
votes: 0


Thanks for the response.

Everything I've seen indicates the syntax should be getElementById(#*$!x).options[0].selected.

I tried it your way just to be sure and it didn't work.

Thanks again,
Glenn

8:54 pm on Dec 9, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


Yes, syntax looks fine. Don't know why I questioned that earlier. It must be something else ... are you certain you have id attributes set up for each of your select elements to correspond with the array you defined?
9:17 pm on Dec 9, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 26, 2006
posts:41
votes: 0


I'm an idiot. Everything works once I made sure there actually were id's for getElementById to interact with! D'oh!

The code only had names, and once I added the id's it all worked as expected.

Thanks,
Glenn