drenl
drenl

Reputation: 1333

jQuery - apply "selected" to option of specific value

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>

Upvotes: 1

Views: 623

Answers (3)

Abinash Dhal
Abinash Dhal

Reputation: 86

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);

  

Upvotes: 4

Ash
Ash

Reputation: 2585

$(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/

Upvotes: 2

Gardezi
Gardezi

Reputation: 2842

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

Upvotes: 2

Related Questions