Skip to content

Instantly share code, notes, and snippets.

@dave1010
Created December 12, 2010 19:33
Show Gist options
  • Save dave1010/738260 to your computer and use it in GitHub Desktop.
Save dave1010/738260 to your computer and use it in GitHub Desktop.
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]');');
@SilverPreece
Copy link

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