Created
December 12, 2010 19:33
-
-
Save dave1010/738260 to your computer and use it in GitHub Desktop.
jQuery plugin to detach (hide) select options and add them again.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]');'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.