On May 7, 9:42 am, "Michael B.  Williams" <mbw...@gmail.com> wrote:
> Consider the following select box:
>
> <select id="test">
> <option value=""></option>
> <option label="XS" value="1">XS</option>
> <option label="S" value="2">S</option>
> <option label="M" value="3">M</option>
> <option label="L" value="4">L</option>
> <option label="XL" value="5">XL</option>
> <option label="0" value="6">0</option>
> <option label="1" value="7">1</option>
> <option label="2" value="8">2</option>
> <option label="3" value="9">3</option>
> <option label="4" value="10">4</option>
> <option label="5" value="11">5</option>
> <option label="6" value="12">6</option>
> <option label="7" value="13">7</option>
> <option label="8" value="14">8</option>
> <option label="9" value="15">9</option>
> <option label="10" value="16">10</option>
> <option label="11" value="17">11</option>
> <option label="12" value="18">12</option>
> </select>
>
> If I do a $("#test").val(2) on this I would hope it would select "S",
> but instead it selects "2". Why is this setting the value of the
> select box based on the label rather than based on the value
> attribute?

It isn't, it is selecting the option with text 2, which just happens
to be the one with a label of 2.

jQuery works backward through the set of options to find the first
with either a text or value property equal to the supplied value and
sets the first that it finds to selected.  That's a pretty crappy
algorithm,  a sensible method would be to first check all options for
a matching value, then for matching text, and check from the first
option onward, not backwards.


--
Rob

Reply via email to