Add item to beginning / top of dropdown list
How to one ensure the following will add an option to a dropdownlist, but at the top of the list? (since the list will already be populated in my case)
Code:
$.fn.prependSelect = function (value, text, selected) {
return this.each(function () {
if (this.tagName == 'SELECT') {
var dropdownList = this;
var option = new Option(text, value, selected);
if ($.browser.msie) {
dropdownList.add(option);
} else {
dropdownList.add(option, null);
}
}
});
};
Thanks in advance!
Re: Add item to beginning / top of dropdown list
Since you're already using jQuery, just go the whole hog and use the jQuery DOM methods to do this. Any performance hit has already been taken by loading jQuery in the first place.
You would want either prepend() or prependTo(). $.browser is deprecated, so you shouldn't be using it; and in this instance you don't actually need it. jQuery methods should handle all the cross-browser bugs internally.
Example:
http://jsfiddle.net/tr333/Pk78H/1/