IE6: get value of a DOM select element
-
13-09-2019 - |
Question
So I'm having a crossbrowser javascript issue.
I've got a <select>
dom element that has some descendent <option>
element with selected=true
.
In Firefox, I can just do select_elt.value
to get the value of the selected option, but this seems not to work in IE6 (which I need to support).
I tried to iterate through the select_elt.getElementsByTagName('option')
to find the selected <option>
, which I could do, but option_elt.value
still doesn't give me the value of that option.
So what is the appropriate way to get the value of an option or select element in IE6?
(yes, I know I should switch to jQuery or some other crosssplatform library, and I may yet, but now I'm curious about how this is done at all in IE6)
Solution
This is the most cross-browser compatible way (in my experience) to do that:
var mySelect = document.getElementById('mySelect');
alert(mySelect.options[mySelect.selectedIndex].value);
OTHER TIPS
Found it in jquery:
(option_elt.attributes.value || {}).specified ? option_elt.value : option_elt.text
Try this:
select_elt.options[select_elt.options.selectedIndex].value