Skip to content

Instantly share code, notes, and snippets.

@michaeldoye
Last active October 27, 2015 09:54
Show Gist options
  • Save michaeldoye/e2159c98b84f05ab19aa to your computer and use it in GitHub Desktop.
Save michaeldoye/e2159c98b84f05ab19aa to your computer and use it in GitHub Desktop.
customFilter
/*
* Requires Bootstrap CSS for .hide and .show
* or your own CSS, or use $(this).hide() & $(this).show()
* @ listObj is the list to be sorted
*************************************************
* Example Usage: $('#input').customFilter('#ul');
*************************************************
*/
jQuery.fn.customFilter = function(listObj) {
var $list = listObj;
var $input = this;
var $lis = $list.children();
$input.on('input', function() {
var term = $input.val();
$.each($lis, function() {
isContains = $(this).text().indexOf(term) > -1;
if( isContains ) {
$(this).addClass("show").removeClass("hide");
} else {
$(this).addClass("hide").removeClass("show");
}
});
});
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment