I feel like this should not be this complicated, but I'm sure I'm missing something trivial. Thanks for anyone that can help!
XSL:
<select id="mySelectList">
<option id="1" description="Blue" />
<option id="2" description="Black" />
<option id="3" description="Green" />
</select>
JS:
$("#mySelectList option[id='1']").attr("selected", "selected");
EDIT: Fixed my select per Nick's response.
Using the jQuery change() method; you can set the selected value of dropdown in jquery by using id, name, class, and tag with selected html elements; see the following example for that: Example 1 :- Set selected value of dropdown in jquery by id.
Syntax of jQuery Select Option$(“selector option: selected”); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $(“selector option: selected”).
You can select on any attribute and its value by using the attribute selector [attributename=optionalvalue] , so in your case you can select the option and set the selected attribute. $("div. id_100 > select > option[value=" + value + "]"). prop("selected",true);
A <select>
should look like:
<select id="mySelectList">
Or if you literally want the element and that's XML, remove the #
for an element selector, like this:
$("mySelectList option[id='1']").attr("selected", "selected");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With