0

i have a dropdown list and want to set the default option by its text(not value), the drop down list is:

<select class="size">
  <option value="0">size0</option>
  <option value="1">size1</option>
  <option value="2">size2</option>
</select>

i want to set the default value to size1, and i tried the following code but none of them works:

$(@el).find('.size>option').filter(=>$(this).text() == 'size1').prop('selected', true)
$(@el).find('.size>option[text="size1"]').attr('selected', true)
$(@el).find('.dimension_post option[text="size1"]').attr('selected', true)

thanks

1
  • Does the common selector find the <option>s -- $(@el).find('.size>option')? Commented May 9, 2014 at 18:06

1 Answer 1

1

You can use the :contains() selector to find an element by its text content.

Assuming $(@el).find('.size > option') finds the <option>s:

$(@el).find('.size > option:contains(size1)').prop('selected', true);

http://jsfiddle.net/FU6xw/

Sign up to request clarification or add additional context in comments.

1 Comment

what if i want to select by exact text? for example if there are two optiosn: size1 and size11, then the above wont' work correctly

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.