select dropdown option by value using jquery

Posted by user1765862 on Stack Overflow See other posts from Stack Overflow or by user1765862
Published on 2014-08-24T09:43:03Z Indexed on 2014/08/24 10:20 UTC
Read the original article Hit count: 171

Filed under:
|
|

I have select following html structure

<select id="mainMenu" data-mini="true" name="select-choice-min">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
   <option value="4">4</option>
   <option value="5">5</option>
   <option value="6">6</option>   
</select>

on page load I want initially set option with value 5 so I tried (but nothing changed)

$(document).ready(function () {
   $('#mainMenu option').eq(5).prop('selected', true);
})

I'm using jquery 1.8.3 and jqueyr mobile since this is mobile website

<script src="/Scripts/jquery-1.8.3.js"></script>
<script src="/Scripts/jquery.mobile-1.4.2.js"></script>

UPDATE: I just realize that every code from posted answers works (as well as mine), #mainMenu option value is set to desired value (5) but it's text doesnt change (in any of this scenarios). Is this normal and is there workarround for this.

I tried $("mainMenu").text("5");

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery