Skip to content

Instantly share code, notes, and snippets.

@donut
Created March 22, 2009 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donut/83079 to your computer and use it in GitHub Desktop.
Save donut/83079 to your computer and use it in GitHub Desktop.
/***********************************************************/
/* LiveFilter Plugin */
/* Version: 1.0 */
/* Mike Merritt */
/* Updated January 4th, 2009 */
/***********************************************************/
(function($){
$.fn.liveFilter = function (filter_field, items, text) {
// Grabs the id of the element containing the filter
var haystack = $(this);
// Listen for the value of the input to change
$(filter_field).keyup(function() {
// Grab the current value of the filter
var needle = $(this).val();
// Hide all elements that do not contain the filter string
haystack.children(items).children(text+':not(:inContains("'+needle+'"))').parent(items).hide();
// Show already hidden elements that do contain the filter string
haystack.children(items).children(text+'::inContains("'+needle+'")').parent(items).show();
});
// Used for a case insensitive :contains selector
jQuery.extend(
jQuery.expr[':'], {
inContains : function(a,b,m) {
return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0; }
});
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment