Why can't Firefox 1.5 parse the DOM children of the SELECT drop down?
I'm required to use Firefox 1.5 (yes I know...) for an in house tool, only used on a closed system.
I have a basic html select drop down menu:
<select name="selectName" ID="Select">
<option value="A" selected>Tom</option>
<option value="B">Jim</option>
<option value="C">Nancy</option>
</select>
In JavaScript, I would like to extract the name (inner text) of the selected option.
In Firefox 3.6 I can do the following:
var x = document.getElementById("Select");
var name = x.children[x.selectedIndex].text;
and name will contain the string of th开发者_JAVA技巧e selected drop down. (Tom
by default)
However, when I run this code in Firefox 1.5, I get the following error:
x.children has no properties
Unfortunately, I can't install Firebug on 1.5, as it is not compatible, to get any more information.
I have a JavaScript Error console on 1.5 and that is all.
How else can I get the text of the selected option, that would work with FF1.5.
Is there a JQuery method that would work in this situation?
Also, I'm interested why this does not work in FF1.5
(Is there a compatible JS debugger for FF 1.5?)
Thank You.
The <select>
element has an "options" attribute whose value is the array of <option>
elements. You should be able to use that exactly as you're using "children". (In fact I think that's a better habit to be in, because the children of the <select>
aren't necessarily just <option>
elements.)
x.options[x.selectedIndex].innerHTML
And go here to get an old version of Firebug that is compatible with Firefox 1.5 (looks liek Firebug 1.05 is the latest version that was compatible):
https://addons.mozilla.org/en-US/firefox/addon/1843/versions/
Shouldn't you be using "childNodes" not "children"? (yes you can use "options", but might as well know a general solution) Another option (heh) is select.getElementsByTagName("OPTION").
精彩评论