I have a select control, and in a javascript variable I have a text string.
Using jQuery I want to set the selected element of the select control to be the item with
Try this...to select the option with text myText
$("#my-Select option[text=" + myText +"]").prop("selected", true);
This line worked:
$("#myDropDown option:contains(myText)").attr('selected', true);
I haven't tested this, but this might work for you.
$("select#my-select option")
.each(function() { this.selected = (this.text == myVal); });
I had a problem with the examples above, and the problem was caused by the fact that my select box values are prefilled with fixed length strings of 6 characters, but the parameter being passed in wasn't fixed length.
I have an rpad function which will right pad a string, to the length specified, and with the specified character. So, after padding the parameter it works.
$('#wsWorkCenter').val(rpad(wsWorkCenter, 6, ' '));
function rpad(pStr, pLen, pPadStr) {
if (pPadStr == '') {pPadStr == ' '};
while (pStr.length < pLen)
pStr = pStr + pPadStr;
return pStr;
}
Here is very simple way. plz use it
$("#free").val("y").change();
Heres an easy option. Just set your list option then set its text as selected value:
$("#ddlScheduleFrequency option").selected(text("Select One..."));