You can do this:
$('#mySelect option:selected').before($('<option>', {
value: 1,
text: 'My option'
}));
Question
I have a select list which is dynamically populated, and an insert button. When I click an option in the select list and then click the insert button, a popup opens. In the popup there is a text field where I insert a value and click the OK button of the popup. Now I want that when I click OK, the popup value should be inserted before the selected option of the select list.
/* Please show some example Code Here */
How do I do this?
Solution
You can do this:
$('#mySelect option:selected').before($('<option>', {
value: 1,
text: 'My option'
}));
OTHER TIPS
try
var sel = opener.document.getElementById("someSelect");
sel.options[sel.selectedIndex-1].option=new Option("some text","some value");
$('#ddlWeeklyWeightIn').on("change", function () {
$("#ddlWeeklyWeightIn option:selected").insertBefore($("#ddlWeeklyWeightIn option:selected").prev());
});
Hope It helps you.