views:

11

answers:

1

For example, I would like to append selected="selected" to the option that contains This one, which jQuery selector should be used to append attributes into that element?

<select>
    <option>Not this</option>
    <option>This one</option>
</select>

Thanks in advance!

+1  A: 

If there are no value attributes, then the text becomes the value, so you can just use .val():

$("select").val("This one");

Or, if it has a value="something" attribute, use that instead:

$("select").val("something");
Nick Craver
So I should use $("select").val("This one").attr('selected', 'selected'); to append it?
@user435216 - nope just `.val("value")`, this sets which one is selected (setting the attribute behind the scenes) :)
Nick Craver