John
John

Reputation:

How to preselect option in select box using jQuery

I have one select box with various options.

When a page loads then one option with a value, for example 10, should be preselected with jQuery.

How can I do that?

Upvotes: 16

Views: 47726

Answers (4)

Kevin Lieser
Kevin Lieser

Reputation: 977

In the latest jQuery version this works for me:

$("option[value='10']").prop('selected',true);

Upvotes: 1

purab
purab

Reputation: 211

Above code is working you can use following code:

$("option[value='10']").attr('selected','selected');

Upvotes: 0

Adam
Adam

Reputation: 44939

When the page loads run this (you can put it in <body onload="//put code here">):

$("option[value='10']").attr('selected','selected');

Upvotes: 27

user113716
user113716

Reputation: 322502

Test the example: http://jsfiddle.net/VArCZ/

$(document).ready(function() {

    $('#mySelectBox').val('10');

});

Although, jQuery wouldn't be required for this if the intial value will be static:

<select id='mySelectBox'>
    <option value='5'>5</option>
    <option value='10' selected='selected'>10</option>
    <option value='15'>15</option>
</select>​

Upvotes: 24

Related Questions