Setting the selected attribute on a select list using jQuery

JqueryHtmlHtml Select

Jquery Problem Overview


I have the following HTML:

<select id="dropdown">
    <option>A</option>
    <option>B</option>
    <option>C</option>
</select>

I have the string "B" so I want to set the selected attribute on it so it will be:

<select id="dropdown">
    <option>A</option>
    <option selected="selected">B</option>
    <option>C</option>
</select>

How would I do this in jQuery?

Jquery Solutions


Solution 1 - Jquery

If you don't mind modifying your HTML a little to include the value attribute of the options, you can significantly reduce the code necessary to do this:

<option>B</option>

to

<option value="B">B</option>

This will be helpful when you want to do something like:

<option value="IL">Illinois</option>

With that, the follow jQuery will make the change:

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

If you decide not to include the use of the value attribute, you will be required to cycle through each option, and manually check its value:

$("select option").each(function(){
  if ($(this).text() == "B")
    $(this).attr("selected","selected");
});

Solution 2 - Jquery

<select id="cars">
<option value='volvo'>volvo</option>
<option value='bmw'>bmw</option>
<option value='fiat'>fiat</option>
</select>

var make = "fiat";

$("#cars option[value='" + make + "']").attr("selected","selected");

Solution 3 - Jquery

If you are using JQuery, since the 1.6 you have to use the .prop() method :

$('select option:nth(1)').prop("selected","selected");

Solution 4 - Jquery

I'd iterate through the options, comparing the text to what I want to be selected, then set the selected attribute on that option. Once you find the correct one, terminate the iteration (unless you have a multiselect).

 $('#dropdown').find('option').each( function() {
      var $this = $(this);
      if ($this.text() == 'B') {
         $this.attr('selected','selected');
         return false;
      }
 });

Solution 5 - Jquery

You can follow the .selectedIndex strategy of danielrmt, but determine the index based on the text within the option tags like this:

$('#dropdown')[0].selectedIndex = $('#dropdown option').toArray().map(jQuery.text).indexOf('B');

This works on the original HTML without using value attributes.

Solution 6 - Jquery

This can be a solution

$(document).on('change', 'select', function () {
            var value = $(this).val();
            $(this).find('option[value="' + value + '"]').attr("selected", "selected");
        })

Solution 7 - Jquery

You can use pure DOM. See http://www.w3schools.com/htmldom/prop_select_selectedindex.asp

document.getElementById('dropdown').selectedIndex = 1;

but jQuery can help:

$('#dropdown').selectedIndex = 1;

Solution 8 - Jquery

Code:

var select = function(dropdown, selectedValue) {
    var options = $(dropdown).find("option");
    var matches = $.grep(options,
        function(n) { return $(n).text() == selectedValue; });
    $(matches).attr("selected", "selected");
};

Example:

select("#dropdown", "B");

Solution 9 - Jquery

$('#select_id option:eq(0)').prop('selected', 'selected');

its good

Solution 10 - Jquery

Something along the lines of...

$('select option:nth(1)').attr("selected","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
QuestionRupertView Question on Stackoverflow
Solution 1 - JquerySampsonView Answer on Stackoverflow
Solution 2 - JqueryDarren RoseView Answer on Stackoverflow
Solution 3 - JqueryG.BusatoView Answer on Stackoverflow
Solution 4 - JquerytvanfossonView Answer on Stackoverflow
Solution 5 - JqueryDerek VeitView Answer on Stackoverflow
Solution 6 - JqueryReganView Answer on Stackoverflow
Solution 7 - JquerydanielrmtView Answer on Stackoverflow
Solution 8 - JqueryJoe ChungView Answer on Stackoverflow
Solution 9 - JqueryMikulascheView Answer on Stackoverflow
Solution 10 - JqueryDavidView Answer on Stackoverflow