[thelist] Deselecting a select element in JS

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


jeff,

<snip>
where do you see that it says that a negative number is invalid?
</snip>
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.

<snip>
 here's an exercise for you.  make a page with a select with no options
selected.
</snip>
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);
}
</script>
<form>
<select id="s">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
  <option>8</option>
</select>
<input type="button" value="deselect" onclick="deselect()">
</form>

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

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