Kwissle

My real-time quiz battle game Kwissle.com

Crosstips.org

My fun Crossword solver project. Crosstips.org & Krysstips.se

Kung Fu

Fujian White Crane Kung Fu

Photos

Photoalbum, both old and new.

Twitter

Follow me on Twitter

Contact me

My contact details and how to contact me.

 

KungFuPeople.com
Do you train Kung Fu?
Or know someone who does?
Then check out KungFuPeople.com


Mobile version of this page Mobile version of this page


 
Web development

Careful when dealing with options in IE


14th of 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.

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.



Comment

Anonymous - 13th July 2011  [«« Reply to this]
Thanks mate!
Ebony - 23rd July 2011  [«« Reply to this]
Slam duiknn like Shaquille O'Neal, if he wrote informative articles.
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.