In the first step, I have a set of values which are fed to a dropdown.
In the next step, I want to SET the value of the dropdown from the set of the values which are fed to it in the first step.
Could anyone let me know the syntax in jquery?
In the first step, I have a set of values which are fed to a dropdown.
In the next step, I want to SET the value of the dropdown from the set of the values which are fed to it in the first step.
Could anyone let me know the syntax in jquery?
If it is, say, the 4th item in the dropdown, you can say:
$('select>option:eq(3)').prop('selected', true); //zero-based
Note that older versions of jQuery used .attr instead of .prop
Here are some other methods:
Imagine a select box like this:
<select name="options">
<option value="1">Red</option>
<option value="2" selected="1">Green</option>
<option value="3">Blue</option>
</select>
You can get the 3rd value this way:
$('[name=options]').val( 3 );//To select Blue
Using the text:
$('[name=options] option').filter(function() {
return ($(this).text() == 'Blue'); //To select Blue
}).prop('selected', true);
Reset it this way:
$('[name=options]').val( '' );