Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jQuery plugin to detach (hide) select options and add them again.
// this is because of http://stackoverflow.com/questions/4398966/how-can-i-hide-select-options-with-javascript-cross-browser/4423543
(function($){
$.fn.extend({detachOptions: function(o) {
var s = this;
return s.each(function(){
var d = s.data('selectOptions') || [];
s.find(o).each(function() {
d.push($(this).detach());
});
s.data('selectOptions', d);
});
}, attachOptions: function(o) {
var s = this;
return s.each(function(){
var d = s.data('selectOptions') || [];
for (var i in d) {
if (d[i].is(o)) {
s.append(d[i]);
console.log(d[i]);
// TODO: remove option from data array
}
}
});
}});
})(jQuery);
// example
$('select').detachOptions('.removeme');
$('.b').attachOptions('[value=1]');');
@GregJPreece

This comment has been minimized.

Copy link

@GregJPreece GregJPreece commented Oct 3, 2012

Great example, was a perfect start for me. I completed the TODO in this example - see my fork of this gist. My modification adds the removal of attached elements from the data() array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment