0

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 !!!

4 Answers4

3

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');
Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318
1
$(".ddl [value=1]").prop("selected", true);
Rajaprabhu Aravindasamy
  • 64,912
  • 15
  • 96
  • 124
Hoijof
  • 1,253
  • 15
  • 27
0

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');
John Stephen
  • 1,961
  • 3
  • 24
  • 47
0
$('select').val(1); //To select Apple
Dynamic
  • 1,473
  • 2
  • 18
  • 23