homepage Welcome to WebmasterWorld Guest from 67.202.56.112
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
document.getElementById proofread help
what am I missing?
pillsbur




msg:3802823
 8:22 pm on Dec 8, 2008 (gmt 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

 

coopster




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

Syntax?
document.getElementById(theElements[i])[0].selected = true;

pillsbur




msg:3802903
 9:46 pm on Dec 8, 2008 (gmt 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

coopster




msg:3803720
 8:54 pm on Dec 9, 2008 (gmt 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?

pillsbur




msg:3803749
 9:17 pm on Dec 9, 2008 (gmt 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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved