If you had lots of options, or this code needed to be run very frequently, then you should look into using a DocumentFragment instead of modifying the DOM many times unnecessarily. For only a handful of options, I'd say it's not worth it though.
DocumentFragment is good option for speed enhancement, but we cannot create option element using document.createElement('option') since IE6 and IE7 are not supporting it.
What we can do is, create a new select element and then append all options. Once loop is finished, append it to actual DOM object.
With the plugin: jQuery Selection Box. You can do this:
With no plug-ins, this can be easier without using as much jQuery, instead going slightly more old-school:
If you want to specify whether or not the option a) is the default selected value, and b) should be selected now, you can pass in two more parameters:
And also, use .prepend() to add the option to the start of the options list. http://api.jquery.com/prepend/
Without using any extra plugins,
If you had lots of options, or this code needed to be run very frequently, then you should look into using a DocumentFragment instead of modifying the DOM many times unnecessarily. For only a handful of options, I'd say it's not worth it though.
------------------------------- Added --------------------------------
DocumentFragment
is good option for speed enhancement, but we cannot create option element usingdocument.createElement('option')
since IE6 and IE7 are not supporting it.What we can do is, create a new select element and then append all options. Once loop is finished, append it to actual DOM object.
This way we'll modify DOM for only one time!
You may want to clear your DropDown first $('#DropDownQuality').empty();
I had my controller in MVC return a select list with only one item.
try this Function: