tags:

views:

2091

answers:

5

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 attrribute 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?

A: 

You can use pure DOM. See http://www.w3schools.com/htmldom/prop%5Fselect%5Fselectedindex.asp

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

but jQuery can help:

$('#dropdown').selectedIndex = 1;
danielrmt
This presumes that you know which element has the desired value. That's not always the case when the list is generated dynamically.
tvanfosson
A: 

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;
      }
 });
tvanfosson
Is there a reason to use .find() rather than $('#dropdown option')? More efficient? The "var $this" stuff also looks wrong to me ...
Bobby Jack
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.
tvanfosson
A: 

Something along the lines of...

$('select option:nth(1)').attr("selected","selected");
David Liddle
+7  A: 

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");
});
Jonathan Sampson
After adding the `value` attributes, the selection can also be done like this: `$("#dropdown").val("B");`
Jørn Schou-Rode
+1 - this is exactly what i was looking for. also, +1 @jorn
Jason
Hi *@Jonathan Sampson*: Nice solution. All other things recommend setting .val() but that just didn't seem to work for me.
MikeSchinkel
A: 

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