Using proper DOM with dynamic select arrays

N

news

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
 
R

RobB

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

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?
 
N

news

RobB wrote:
....
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top