Forum Moderators: open

Message Too Old, No Replies

Javascript Option Replacement

Use JS to replace the options in a select

         

tprogers

6:09 pm on Oct 14, 2005 (gmt 0)

10+ Year Member



I'm using JavaScript to replace the options in a select box based on the current value of another select. I just discovered that the technique I'm using to do this doesn't work quite right on MacIE 5.2, which is a browser I'm trying to support.

Here is my implementation. Currently, the script runs, but none of the options have any text in them. Suggestions are appreciated.

Given:

objSelect
is the select box whose options I'm messing with;
arrChoice
is an array representing the value and text I want to add to the select (
arrChoice[0]
is used by the matching script to screen entries).

var objOption = document.createElement("OPTION"); 
objSelect.options.add(objOption);
objOption.value = arrChoice[1];
objOption.text = arrChoice[2];

Bernard Marx

6:36 pm on Oct 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's a mix old-style and W3C methods. I don't have a mac, but maybe it's worth trying using 100% old-style.

var objOption = new Option(arrChoice[2],arrChoice[1]);
objSelect.options.add(objOption);
// maybe replace line above with
// objSelect.options[objSelect.options.length] = objOption;

Have a go.

Ref: [devguru.com...]

tprogers

8:03 pm on Oct 14, 2005 (gmt 0)

10+ Year Member



Thanks!

This works perfectly in FF/IE and MacIE. I haven't tested Opera yet, but I'm hopeful.

tprogers

5:52 pm on Nov 1, 2005 (gmt 0)

10+ Year Member



Addendum: this method works perfectly in Opera, also.