[thelist] Deselecting a select element in JS

.jeff jeff at members.evolt.org
Thu Mar 21 14:26:01 CST 2002


andrew,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: Hardacker, Andrew
>
> > 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.
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

hmmm, don't have that book handy (it's at the office).

instead, here's what netscape has to say about the issue in their js docs:

http://developer.netscape.com/docs/manuals/js/client/jsref/select.htm#1193420

and what msdn has to say about it:

http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/selectedindex.asp

even the w3 has something to say about it:

http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-85676760

none of these references say the value you can set must be a positive integer.  in fact, the w3 refers to the value being of type long.  i'm not familiar with their terminology of datatypes.  it would seem to be a reference to a long-int.  if memory serves, long-int can be positive or negative.  anybody care to enlighten me?

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> > here's an exercise for you.  make a page with a select
> > with no options selected.
>
> 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*)
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

fair enough.  when the word netscape is used, i automatically think nn4.  want me to think nn6?  say mozilla or nn6.  in nn4, setting selectedIndex to -1 works.

indeed nn6 pukes on the select-one on the test page i put up, though it works flawlessly with the select-multiple.  unless someone can point to a definitive resource that says "selectedIndex can't be a negative number" or "you can't unselect all options in a select-one", i'll consider it a bug in nn6 and proceed accordingly.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> This works in Netscape6, but not IE. What's wrong?
>
> 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);
> }
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

it doesn't work in ie because setting the individual selected properties of each individual option doesn't cause the selectedIndex property on the select to be changed (unless you were to set selected to true on one or more of the options).  this is at least the case with selects of type select-one.  with selects of type select-multiple, setting the selected property seems to work as expected.

fwiw, i don't see any point in referencing form elements via getElementyById() as this unnecessarily breaks form interaction for older browsers.  there's a standard dom in place for forms, elements, images, etc., why complicate the issue?

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

and here we were just starting to trot out the nuances of the various browsers for all to see.  *grin*

don't go just yet.

.jeff

http://evolt.org/
jeff at members.evolt.org
http://members.evolt.org/jeff/




More information about the thelist mailing list