Careful when dealing with options in IE
14 April 2006
If you have a drop down that looks like this:
<select name="name" onchange="alert(this.options[this.selectedIndex].value)"> <option>Bill</option> <option>Gates</option> </select>
you'll get a different alert message in Firefox and IE6. Try it here
Firefox will understand that if you don't have a
value attribute on a
option tag, it takes the content of the
option tag as the value. IE doesn't. On the above code it returns blank (aka. empty string) in the alert.
var selectelement = document.forms.name; var optionelements = selectelements.options; var selected_option = optionelements[selectelement.selectedIndex]; var value; if (selected_option.getAttribute('value')==null) value = selected_option.firstChild.nodeValue; else value = selected_option.value; alert(value);
...or something like that. But who can be asked? I just made sure my
option tags always have a
value attribute even though the XHTML 1.0 DTD does not require it.