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
  • 331,213
  • 40
  • 305
  • 339
1
$(".ddl [value=1]").prop("selected", true);
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
Hoijof
  • 1,343
  • 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');
Earth
  • 3,477
  • 6
  • 37
  • 78
0
$('select').val(1); //To select Apple
Dynamic
  • 1,553
  • 2
  • 19
  • 25