homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
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?

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


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!




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

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


 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,


 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?


 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.


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.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved