|
-
Sep 24th, 2012, 04:22 PM
#1
Thread Starter
Hyperactive Member
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!
-
Nov 3rd, 2012, 05:36 AM
#2
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/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|