IE-compatible way to change select options with jQuery
(function($, window) {
$.fn.replaceOptions = function(options) {
var self, $option;
this.empty();
self = this;
$.each(options, function(index, option) {
$option = $("<option></option>")
.attr("value", option.value)
.text(option.text);
self.append($option);
});
};
})(jQuery, window);
It expects an array of objects which contain "text" and "value" keys. So usage is as follows:
var options = [
{text: "one", value: 1},
{text: "two", value: 2}
];
$("#foo").replaceOptions(options);
Written by Kamil Tusznio
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ie
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#