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!

0

3 Answers 3

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

Comments

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

Comments

0

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

taken from

jQuery Set Select Index

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.