What about setting the value with the val method?

$('select').val('theValue');

Kirk Cerny

On Sat, Sep 26, 2009 at 12:06 PM, Eric Faerber <[email protected]> wrote:
> Wade Preston Shearer wrote:
>> In case someone else needs to do this, I'll share something I just
>> learned (read: wasted a bunch of time on):
>>
>> If you want to dynamically select an option within a select element,
>> the following won't work (using jQuery):
>>
>> $('option').attr('selected', 'selected');
>>
>>
>> You have to use selectedIndex on the select, like this:
>>
>> $('select').get(0).selectedIndex = value;
>>
>>
>> It's interesting that the first option doesn't work since this does:
>>
>> $('input').attr('disabled', 'disabled');
>>
> http://docs.jquery.com/Attributes/val#overview
>
> $("select").*val*("options value");
>
> The first code that you posted won't work because that is selecting
> every <option> on the page and setting it to selected="selected". If you
> have a single select <select> then it just defaults to the first
> <option> if all of them are selected="selected".
>
> $('option').get(0).attr('selected', 'selected');
>
> That might work (haven't tested) since you are getting the first
> <option> and setting it to selected.
>
> The disable works because you are setting <input> to be disabled. There
> are multiple <inputs> underneath an <option> or something like that to
> mess it up.
>
> _______________________________________________
>
> UPHPU mailing list
> [email protected]
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net
>

_______________________________________________

UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net

Reply via email to