66

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?

10 Answers 10

141

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");
});
Sign up to request clarification or add additional context in comments.

10 Comments

After adding the value attributes, the selection can also be done like this: $("#dropdown").val("B");
Setting .val() WILL NOT call .change() event binded to your <select>. Just saying...
If you want to fire the .change() event binded to your select you can use $(this).change()
@madtyn $.fn.prop didn't get added until 2011. This answer predates that by a few years. Additionally, though not my intention, by changing the attribute you can enable greater control over styling via attribute selectors.
.val() and .prop() don't change/set the attribute. But this behavior is required in some cases and is only done with .attr() jsfiddle.net/6sebLnrz
|
15
<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");

Comments

14

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

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

1 Comment

This solution is best. .attr() doesn't allow multiple changes. .prop() does.
4

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

2 Comments

Is there a reason to use .find() rather than $('#dropdown option')? More efficient? The "var $this" stuff also looks wrong to me ...
I think the difference is mainly stylistic. Under the hood I would expect them to operate basically the same way. Get a collection, filter it. I've started using $this as a way to hold a reference to the jQuery object. I've also seen self (or $self), but I think that's less clear.
2

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.

Comments

2

This can be a solution

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

Comments

1

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;

1 Comment

This presumes that you know which element has the desired value. That's not always the case when the list is generated dynamically.
1

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

Comments

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

its good

Comments

0

Something along the lines of...

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.