views:

235

answers:

3

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)

A: 

Found it in jquery:

(option_elt.attributes.value || {}).specified ? option_elt.value : option_elt.text
rampion
+1  A: 

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);
Jage
A: 

Try this:

select_elt.options[select_elt.options.selectedIndex].value
andreas