Question

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

Was it helpful?

Solution

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top