I got this litte script that works perfect to populate a dropdownlist. No worries here:
// ----- Populate a select list with given data ---- //
$.fn.fillSelect = function (data) {
return this.clearSelect().each(function () {
if (this.tagName == 'SELECT') {
var dropdownList = this;
$.each(data, function (index, optionData) {
var option = new Option(optionData.Text, optionData.Value, optionData.Selected);
if ($.browser.msie) {
dropdownList.add(option);
} else {
dropdownList.add(option, null);
}
});
}
});
};
However I wanted to add another method to the plugin to "prepend" a given list and can't get it working (keep telling me .prepend is not a function?)
// ----- Prepend the selectlist with the given data ---- //
$.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.prepend(option);
} else {
dropdownList.prepend(option, null);
}}
});
};
krokonoster
Contributor
4291 Points
1352 Posts
DropDown add works, but not prepend?
Oct 07, 2012 08:07 PM|LINK
Hi,
I got this litte script that works perfect to populate a dropdownlist. No worries here:
// ----- Populate a select list with given data ---- // $.fn.fillSelect = function (data) { return this.clearSelect().each(function () { if (this.tagName == 'SELECT') { var dropdownList = this; $.each(data, function (index, optionData) { var option = new Option(optionData.Text, optionData.Value, optionData.Selected); if ($.browser.msie) { dropdownList.add(option); } else { dropdownList.add(option, null); } }); } }); };However I wanted to add another method to the plugin to "prepend" a given list and can't get it working (keep telling me .prepend is not a function?)
// ----- Prepend the selectlist with the given data ---- // $.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.prepend(option); } else { dropdownList.prepend(option, null); }} }); };bruce (sqlwo...
All-Star
36850 Points
5445 Posts
Re: DropDown add works, but not prepend?
Oct 07, 2012 11:52 PM|LINK
the select dom object does not have a prepend function, thus the error. use the add(option, beforeIndex)