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