[thelist] Deselecting a select element in JS

Hardacker, Andrew Andrew.Hardacker at Compuware.com
Thu Mar 21 13:36:01 CST 2002


where do you see that it says that a negative number is invalid?
page 716 of Danny Goodman's Dynamic HTML states that the value of this
attribute is a positive integer. I haven't checked the W3.

 here's an exercise for you.  make a page with a select with no options
I did that before I wrote. After setting the selectedIndex to either -1 or
null with JavaScript, Netscape6.2 displayed it as 0. I don't usually rush in
headlong with _completely_ unsubstantiated information. 8*)

This works in Netscape6, but not IE. What's wrong?

<script type="text/javascript">
function deselect() {
 var s = document.getElementById('s');
 alert("before: selected="+s.selectedIndex);
 for (var i=0;i < s.length; i++) {
  s.options[i].selected = false;
 alert("after: selected="+s.selectedIndex);
<select id="s">
<input type="button" value="deselect" onclick="deselect()">

Thanks. I think I'll go back to lurking.

Andy Hardacker

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