Wednesday 19 October 2016

jquery - Set select option 'selected', by value



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:








I need to select the option with value val2. How can this be done?



Here's a demo page:
http://jsfiddle.net/9Stxb/



Answer



There's an easier way that doesn't require you to go into the options tag:



$("div.id_100 select").val("val2");


Check out the this jQuery method.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...