I have a select
field with some options in it. Now I need to select one of those options
with jQuery. But how can I do that when I only know the value
of the option
that must be selected?
I have the following HTML:
<div class="id_100">
<select>
<option value="val1">Val 1</option>
<option value="val2">Val 2</option>
<option value="val3">Val 3</option>
</select>
</div>
I need to select the option with value val2
. How can this be done?
Here's a demo page: http://jsfiddle.net/9Stxb/
Use:
This works for me. I'm using this code for parsing a value in a fancybox update form, and my full source from app.js is:
And my PHP code is:
$('#graphtype option[value=""]').prop("selected", true);
This works well where
#graphtype
is the id of the select tag.example select tag:
Setting to Pending Status by value
Just put in this code:
There's an easier way that doesn't require you to go into the options tag:
Check out the this jQuery method.
I think the easiest way is selecting to set val(), but you can check the following. See How to handle select and option tag in jQuery? for more details about options.
Not optimised, but the following logic is also useful in some cases.