Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / April 2005



Tip: Looking for answers? Try searching our database.

Using proper DOM with dynamic select arrays

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
news@celticbear.com - 29 Apr 2005 18:13 GMT
Based on a condition, I want to add new options into a form's SELECT
menu.

This method has been working for Firefox, but not IE/Opera/Avant:
document.form1.aem_from[0]=new Option("[ select ]","X");

(Although the Firefox JavascriptConsole would of course tell me it's
not good.)

Trying to get it to work in every other browser than Firefox...how the
heck to I format that using proper DOM?

I've tried:
document.getElementById('aem_from'[0])=new Option("[ select ]","X");
and with various other placements of quotes and positioning of the
array brackets, like:
document.getElementById('aem_from')[0]=new Option("[ select ]","X");
and:
document.getElementById('aem_from[0]')=new Option("[ select ]","X");
and it none of it works in any browser and the Console simply says I
have an "invalid assignment left-hand side".

I tried doing a search on the newsgroup...but I can't find anything
quite like this. Maybe threads that are similar, but I didn't grok it
enough to carry over the lesson given. My understanding of JavaScript
is less "understanding" and more "seething hatred of a necessary evil
in my life." =)

Thanks for any suggestions!
Liam
RobB - 29 Apr 2005 18:33 GMT
n...@celticbear.com wrote:
> Based on a condition, I want to add new options into a form's SELECT
> menu.
[quoted text clipped - 26 lines]
> Thanks for any suggestions!
> Liam

document.getElementById() just gets you the element (object) with that
id set - if it exists - or returns null. Start with that:

document.getElementById('aem_from')

Then reference the first Options[] slot:

document.getElementById('aem_from').options[0]

...and add your new option:

document.getElementById('aem_from').options[0]=new Option("[ select
]","X");

As you can see - you did that already, at least the shorthand version
(no reference to the options collection). Did you give the listbox an
id as well as a name?
news@celticbear.com - 29 Apr 2005 20:15 GMT
...
> document.getElementById('aem_from').options[0]=new Option("[ select
> ]","X");
>
> As you can see - you did that already, at least the shorthand version
> (no reference to the options collection). Did you give the listbox an
> id as well as a name?

That did it. Amazing what just a period will do. =/

Yeah, all the rest of my naming was fine, I just needed to know to put
that period there. =P

Thanks for the reply!!
Liam
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.