user2179026
user2179026

Reputation:

How to make a particular option selected in jquery

I have written html as

<select class="ddl">
<option value="1">Apple</option>
<option value="2">Banana</option>
<option value="3">Cherry</option>
</select>

Now I want that if option is 1 then it should be selected as

<option value="1" selected="selected">Apple</option>

Please help me !!!

Upvotes: 0

Views: 1700

Answers (4)

Earth
Earth

Reputation: 3571

For versions 1.6 and higher you can use,

$('.ddl option:eq(1)').prop('selected', true);

For older versions you can use,

$('.ddl option:eq(1)').attr('selected', 'selected');

Upvotes: 0

Hoijof
Hoijof

Reputation: 1413

$(".ddl [value=1]").prop("selected", true);

Upvotes: 1

Dynamic
Dynamic

Reputation: 1663

$('select').val(1); //To select Apple

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

You can use the val() method to set the selected option in a select, much as you would use it to set the value attribute of a standard input element. Try this:

$('select.ddl').val('1');

Upvotes: 3

Related Questions