Re: Setting selectedIndex before adding option with that index

On Thu, 12 Nov 2009, Geoffrey Sneddon wrote:
> Given the following HTML document:
> <!doctype html>
> <select></select>
> <script>
> var s = document.getElementsByTagName('select')[0];
> s.selectedIndex = 0;
> s.options[s.options.length] = new Option();
> alert(s.selectedIndex);
> </script>
> At the moment the spec says we should alert -1.

It says it should alert 0, due to:

# If the multiple attribute is absent, whenever there are no option 
# elements in the select element's list of options that have their 
# selectedness set to true, the user agent must set the selectedness of 
# the first option element in the list of options in tree order that is 
# not disabled, if any, to true.

On Tue, 15 Dec 2009, Geoffrey Sneddon wrote:
> What is wrong is that adding an option to a select element should set 
> its selectedness to true if there are no options that are already 
> selected.

The spec already requires this.

On Tue, 15 Dec 2009, Geoffrey Sneddon wrote:
> When adding an option element to a select, Firefox sets the selectedness 
> to the first non-disabled element, WebKit sets the selectedness to the 
> zeroith element, and Opera doesn't select anything. Either the Firefox 
> or WebKit behaviour is needed for web compat.

The spec follows Firefox on this.

> Also, I can't see anywhere that the spec requires the first non-disabled 
> (Fx, Opera)/any (WebKit) option to be selected initially. (Though I 
> guess this can be fixed at the same time as the above.)

It's the same sentence, quoted above.

Ian Hickson               U+1047E                )\._.,--....,'``.    fL       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

Received on Friday, 12 February 2010 03:58:57 UTC