[thelist] Deselecting a select element in JS

Hardacker, Andrew Andrew.Hardacker at Compuware.com
Thu Mar 21 12:41:01 CST 2002


<snip>
Quick question -
How do you deselect a select element using JavaScript (IYSWIM) ?
</snip>

I don't believe you can. IE will let you set selectedIndex to a negative
number, but that's technically illegal - it has to be positive - so Netscape
will override any attempt to set selectedIndex to -1 or null with 0, which
corresponds to the first option.

The most common solution is to make the first option "logically"
unselectable. Set the text to blank or "Please make a selection from this
list" and then check to ensure that the selectedIndex is greater than 0.

Andy Hardacker
http://hardacker.com/



The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it.




More information about the thelist mailing list