I would suggest that you do not use the solutions that use a <span> wrapper because it isn't valid HTML, which could cause problems down the road. I think the preferred solution is to actually remove any options that you wish to hide, and restore them as needed. Using jQuery, you'll only need these 3 functions:
The first function will save the original contents of the select. Just to be safe, you may want to call this function when you load the page.
function setOriginalSelect ($select) { if ($select.data("originalHTML") == undefined) { $select.data("originalHTML", $select.html()); } // If it's already there, don't re-set it }
This next function calls the above function to ensure that the original contents have been saved, and then simply removes the options from the DOM.
function removeOptions ($select, $options) { setOriginalSelect($select); $options.remove(); }
The last function can be used whenever you want to "reset" back to all the original options.
function restoreOptions ($select) { var ogHTML = $select.data("originalHTML"); if (ogHTML != undefined) { $select.html(ogHTML); } }
Note that all these functions expect that you're passing in jQuery elements. For example:
// in your search function... var $s = $('select.someClass'); var $optionsThatDontMatchYourSearch= $s.find('options.someOtherClass'); restoreOptions($s); // Make sure you're working with a full deck removeOptions($s, $optionsThatDontMatchYourSearch); // remove options not needed
Here is a working example: http://jsfiddle.net/9CYjy/23/