On Jan 28, 9:59 pm, "Richard Quadling" <[EMAIL PROTECTED]>
wrote:
> On 25/01/2008, Justin Perkins <[EMAIL PROTECTED]> wrote:
>
> > Take the advice of kangax and Richard,
>
> > To set the selected item of a select box, you useselectedIndex.
>
> > There is no better way. Setting the selected attribute on an option is
> > not a reliable means of setting the selected state

I will believe that statement if you post a test case that shows it to
be unreliable.


> >, not to mention it
> > requires you to "unset" the selected attribute on another option.

In a single select, setting the selected property of any particular
option to true will set the selected property of all other option
elements in the select to false.  To set any particular option to
selected, you simply set it's selected property to true.  That will
also set the selectedIndex property of the select element.

Setting the selected property in a multiple select does not change the
value of the selected property of any other option.  The selectedIndex
property of the select element is set to the lowest index of all the
selected options, so changing the selected property of individual
select elements may or may not change the value of the selectedIndex
property.


> Just adding a few notes to this questio (not directly in response to
> Justin's recommendation).
>
> There are effectively 2 type of select boxes.
>
> select-multiple and select-single
>
> They behave slightly different with regard to the "select" properties.
>
> If select-single is used, then $('select').selectedIndexis the property to 
> use.

That opinion seems to be based on the erroneous belief that the
selected property of every other option must be set to false, which is
not true.



> If select-multiple is used, then you $('option_x').selected is the
> property to use.

If the intention is to set more than one option to selected, then the
selected property is the *only* reasonable way to do it[1].  Setting
the select's selectedIndex property will only ever select one option.

If the intention is to select no options, then either set the select's
selectedIndex to -1 or loop over all the option elements and set their
selected property to false.


[...]

> Setting option.selected is perfectly fine if your select is <select
> multiple><opti....></select>

The "best" way to set an option to selected is based on what the OP is
actually trying to do, not necessarily on the type of select element.
If the intention is to set a single option to selected in a multiple
select, then selectedIndex is the best method.

If the intention is to set any particular option to selected in a
single select, then setting the selectedIndex property of the select
OR setting the selected property of the particular option will do
exactly the same thing.



1.  An alert could be used to ask the user to do it themselves, or the
innerHTML property of the select element could be modified to set the
selected attribute of particular option elements.  But I don't
consider those reasonable.


--
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to