Skip to content

Instantly share code, notes, and snippets.

@jesalg
Created December 2, 2011 22:36
Show Gist options
  • Save jesalg/1425167 to your computer and use it in GitHub Desktop.
Save jesalg/1425167 to your computer and use it in GitHub Desktop.
JavaScript List Filter
// Custom css expression for a case-insensitive contains()
$.expr[':'].Contains = function(a,i,m){
return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
};
// Search function for Find Partners
var listFilter = function() {
var input = $("input.filterinput");
var list = $("#list");
$(input).bind('change',function(){
var filter = $(this).val();
if(filter) {
// this finds all li in an ul that contain the input,
// and hide the ones not containing the input while showing the ones that do
$(list).find("li:not(:Contains(" + filter + "))").slideUp();
$(list).find("li:Contains(" + filter + ")").slideDown();
} else {
$(list).find("li").slideDown();
}
return false;
});
$(input).bind('keyup',function(){
// fire the above change event after every letter
$(this).change();
});
}
listFilter();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment