As the question says, how do I add a new option to a DropDownList using jQuery?
Thanks
As the question says, how do I add a new option to a DropDownList using jQuery?
Thanks
With the plugin: jQuery Selection Box. You can do this:
var myOptions = {
"Value 1" : "Text 1",
"Value 2" : "Text 2",
"Value 3" : "Text 3"
}
$("#myselect2").addOption(myOptions, false);
without using any extra plugins,
var myOptions = {
val1 : 'text1',
val2 : 'text2'
};
$.each(myOptions, function(val, text) {
$('#mySelect').append(
$('<option></option>').val(val).html(text)
);
});
With no plug-ins, this can be easier without using as much jQuery, instead going slightly more old-school:
var myOptions = {
val1 : 'text1',
val2 : 'text2'
};
$.each(myOptions, function(val, text) {
$('#mySelect').append( new Option(text,val) );
});
Worked!!I too found the naming convention a little confusing, although it works well for this example. Thanks though!
U can use direct
$"(.ddlClassName").Html("<option selected=\"selected\" value=\"1\">1</option><option value=\"2\">2</option>")
-> Here u can use direct string