0

I have the following option box:

<label for="inputselection1">Input #1:</label>
<select id="inputselection1" name="unittype">
<option value="1">Miner</option>
<option value="2">Puffer</option>
<option value="3">Snipey</option>
<option value="4">Max</option>
<option value="5">Firebot</option>
</select><br>

however, when I execute the following jquery option onload, the text (selected option) does not get changed to Max:

$("#inputselection1 option[text=Max]").attr("selected","selected");

What gives?

Thanks!

user1804633
  • 1,507
  • 2
  • 9
  • 9

3 Answers3

1

Use .val() to set select value by option value:

$("#inputselection1").val(4);​

If you insist on selecting by text, then use this:

$("#inputselection1 option").filter(function() {
    return $(this).text() === 'Max';
}).attr("selected", "selected");
Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
1

If you want to select by option text you can use a filter function

$("#inputselection1 option").filter(function() {
    return $(this).text() === 'Max';
}).prop("selected", true);

Fiddle

Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
0

$("#inputselection1 option[value=4]").attr("selected","selected");

taken from

jQuery Set Select Index

Community
  • 1
  • 1
sayannayas
  • 764
  • 9
  • 15