jQuery to retrieve and set selected option value of html select element

Jquery

Jquery Problem Overview


I am attempting to retrieve and set the selected value of a select element (drop down list) with jQuery.

for retrievel i have tried $("#myId").find(':selected').val(), as well as $("#myId").val() but both return undefined.

Any insight into this problem would be much appreciated.

Jquery Solutions


Solution 1 - Jquery

to get/set the actual selectedIndex property of the select element use:

$("#select-id").prop("selectedIndex");

$("#select-id").prop("selectedIndex",1);

Solution 2 - Jquery

The way you have it is correct at the moment. Either the id of the select is not what you say or you have some issues in the dom.

Check the Id of the element and also check your markup validates at here at W3c.

Without a valid dom jQuery cannot work correctly with the selectors.

If the id's are correct and your dom validates then the following applies:

To Read Select Option Value

$('#selectId').val();

To Set Select Option Value

$('#selectId').val('newValue');

To Read Selected Text

$('#selectId>option:selected').text();

Solution 3 - Jquery

$('#myId').val() should do it, failing that I would try:

$('#myId option:selected').val()

Solution 4 - Jquery

When setting with JQM, don't forget to update the UI:

$('#selectId').val('newValue').selectmenu('refresh', true);

Solution 5 - Jquery

$("#myId").val() should work if myid is the select element id!

This would set the selected item: $("#myId").val('VALUE');

Solution 6 - Jquery

Suppose you have created a Drop Down list using SELECT tag like as follows,

<select id="Country">

Now if you want to see what is the selected value from drop down using JQuery then, simply put following line to retrieve that value..

var result= $("#Country option:selected").text();

it will work fine.

Solution 7 - Jquery

I know this is old but I just had a bear of a time with Razor, could not get it to work no matter how hard I tried. Kept coming back as "undefined" no matter if I used "text" or "html" for attribute. Finally I added "data-value" attribute to the option and it read that just fine.

 <option value="1" data-value="MyText">MyText</option>

 var DisplayText = $(this).find("option:selected").attr("data-value");

Solution 8 - Jquery

$( "#myId option:selected" ).text(); will give you the text that you selected in the drop down element. either way you can change it to .val(); to get the value of it . check the below coding

<select id="myId">
    <option value="1">Mr</option>
    <option value="2">Mrs</option>
    <option value="3">Ms</option>`
    <option value="4">Dr</option>
    <option value="5">Prof</option>
</select>

Solution 9 - Jquery

Try this

$('#your_select_element_id').val('your_value').attr().add('selected');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErnieStingsView Question on Stackoverflow
Solution 1 - JquerypmkoView Answer on Stackoverflow
Solution 2 - JqueryredsquareView Answer on Stackoverflow
Solution 3 - Jquerykarim79View Answer on Stackoverflow
Solution 4 - JqueryJasper GiscombeView Answer on Stackoverflow
Solution 5 - JqueryRigobert SongView Answer on Stackoverflow
Solution 6 - JqueryJaySingView Answer on Stackoverflow
Solution 7 - JqueryDarklokiView Answer on Stackoverflow
Solution 8 - JqueryWikum WView Answer on Stackoverflow
Solution 9 - JquerySyed Nazir HussainView Answer on Stackoverflow