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.

It's not been a problem until today for me but it's worth keeping in mind next time you need to read from a drop down with javascript. Ideally your javascript should do something like this:


var selectelement = document.forms[0].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.

Comments

Anonymous

Thanks mate!

Ebony

Slam duiknn like Shaquille O'Neal, if he wrote informative articles.

Your email will never ever be published.

Previous:
Case insensitive list remove call (part II) April 11, 2006 Python
Next:
Date formatting in Python or in PostgreSQL (part II) April 19, 2006 Python
Related by category:
Fastest way to find out if a file exists in S3 (with boto3) June 16, 2017 Web development
Be very careful with your add_header in Nginx! You might make your site insecure February 11, 2018 Web development
<datalist> looks great on mobile devices August 28, 2020 Web development
Msgpack vs JSON (with gzip) December 19, 2017 Web development
Related by keyword:
How to "onchange" in ReactJS October 21, 2015 React, JavaScript
An AngularJS directive with itself as the attribute September 3, 2014 AngularJS, JavaScript
OpenID, Attribute Exchange, SReg, python-openid and Google April 23, 2010 Python, Web development
The importance of the TITLE attribute April 23, 2008 Web development