HTML
<select id="myDDL">
<option selected="selected" value="0">default</option>
<option value="1">apples</option>
<option value="2">oranges</option>
</select>
Javascript
setFieldValue("myDDL", "apples");
function setFieldValue(field, value)
{
var val = $("#" + field + " option[text]");
console.log(val);
$("#" + field).val(val);
}
JSFiddle
https://jsfiddle.net/nnvh7e43/
I would like to select an option within a select field based on its text, i.e.:
var val = $("#" + field + " option[text='" + value + "']");
However as soon as I add [text] after option the selector seems to select the entire document, whereas $("#" + field + " option"); simply selects all option tags within the select field, as I would expect.
Could you please explain why it does not recognise "[text]" and how I can correct this?
Thanks