Last active
December 18, 2015 10:59
-
-
Save ocombe/5771999 to your computer and use it in GitHub Desktop.
Inline jQuery search filter
This file contains hidden or 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
$(document).ready(function() { | |
// let's make a case insensitive contains selector | |
$.extend($.expr[':'], { | |
'containsi': function(elem, i, match, array) { | |
return (elem.textContent || elem.innerText || '').toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0; | |
} | |
}); | |
}); | |
//filter results based on query | |
function filter(list, selector, filter) { | |
if(filter) { | |
// hide all and show matching elements | |
$(list).hide().filter(function(i) { | |
return $(this).find(selector+":containsi(" + filter + ")").length > 0; | |
}).show(); | |
} else { | |
$(list).show(); // show all | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment