0

Does anyone know how can I set the selected value for dropdown based on the display value in jquery/javascript

Example:

<select id='test'>
  <option value='sgf'>One</option>
  <option value='sdf'>Two</option>
  <option value='gfr'>Three</option>
  <option value='dfg'>Four</option>
</select>

How Can I set the selected to value based on the display value? Let say, I want to set the selected when display value is 'Three'

1
  • So just to clarify, you want to change the value 'gfr' to "Three" once selected? Commented Jun 27, 2011 at 2:38

2 Answers 2

3
$('#test option').filter(function () {
   return $(this).text() == 'Three';
})[0].selected = true;
Sign up to request clarification or add additional context in comments.

Comments

1
$("#test option:contains(Three)").prop("selected", true)

assuming the words are sufficiently unique. Otherwise go with @Box9's approach for more robustness.

2 Comments

this doesnt seem work for me... error 'Object doesn't support this property or method' display
prop was introduced in jQuery 1.6 afaik. What version are you on?

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.