I have a code like this:
var a = selectBox.options[selectBox.selectedIndex];
If I execute:
alert(a);
Using FF, Chrome, Opera, etc… I get:
[object HTMLOptionElement]
Which is exactly what I want.
If I use IE6 or IE7, I get:
[object]
With no expected Option properties at all.
This means that there is no TextNode as a childNode and no value property. How can I solve that problem?
Also if I try to use that node to add to another select box IE throws an exception:
SCRIPT87: Invalid argument.
I was requested to make a program that is compatible with IE6 but I can’t find a way to make it work. I need to move Option objects around and IE7 is not allowing me.
Edit by request:
Context HTML
<select id="identification">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="toBeFilled" multiple>
</select>
Important note:
What I’m making NEVER knows the list of names and ids of the document. It only knows the nodes which it will use to work in the document.
When I state Node I mean, for instance, what document.getElementById() returns.
var a = selectBox.options[selectBox.selectedIndex];will definitely give you anOptionobject (orHTMLOptionElement) in all browsers, IE 7, 6, 5, 4 and possibly 3 included (although IE may not give you a particularly helpful value when you calltoString(), as you’ve observed). This will havetextandvalueproperties that reflect the text and value of the option. If this isn’t working for you then there’s a problem elsewhere in your code.As for transferring options between select boxes, I’m not so sure on this, but if in doubt you could just create a new
Optionand add that to your new select box: