1

Using jQuery, how do I apply "selected" to option value C?

<select id="letters">
    <option value="A">A<option>
    <option value="B">B<option>
    <option value="C">C<option>
    <option value="D">D<option>
</select>
drenl
  • 1,321
  • 4
  • 18
  • 32
  • 1
    Duplicate of https://stackoverflow.com/questions/4680075/set-selected-option-of-select-box , https://stackoverflow.com/questions/13343566/set-select-option-selected-by-value , https://stackoverflow.com/questions/38497852/set-an-option-value-as-selected , https://stackoverflow.com/questions/1280499/jquery-set-select-index , and others. – Brian Gottier Sep 13 '17 at 00:50

3 Answers3

4

As of jQuery 1.9, jQuery has updated changed this functionality.

The "selected" state of an option is actually a property, therefore jQuery has changed this to use the .prop() method.

$('#letters option[value=C]').prop('selected', true);

  

Phani Kumar M
  • 4,564
  • 1
  • 14
  • 26
2

you need to do something like this

$('#yourdropddownid').val('C');

or do something like this

 $('#yourdropddownid').value = 'C';

For more information please view this post

JQuery - how to select dropdown item based on value

Gardezi
  • 2,692
  • 2
  • 32
  • 62
2

$(function(){
 $('#letters').val('C');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="letters">
    <option value="A">A<option>
    <option value="B">B<option>
    <option value="C">C<option>
    <option value="D">D<option>
</select>

Find the object and set the selected value :

$('#letters').val('C');

Check the working fiddle here: https://jsfiddle.net/yvow5030/

Ash
  • 2,575
  • 2
  • 19
  • 27