Skip to content

Instantly share code, notes, and snippets.

@joshbeckman
Created September 19, 2013 15:17
Show Gist options
  • Save joshbeckman/6625059 to your computer and use it in GitHub Desktop.
Save joshbeckman/6625059 to your computer and use it in GitHub Desktop.
Client-side full-text filter in CSS
<input type="text" placeholder="filter" id="filter">
<ul class="contacts">
<li class="filterable" data-index="all my lower case search terms">
All my lower case search terms
</li>
<li class="filterable" data-index="lowerlowercasecase">
Lower lower case case
</li>
<!-- add as much data as you want, add the selector class and add the terms in data-index -->
</ul>
<style id="filter_style"></style>
<script type="text/javascript">
var filterStyle = document.getElementById('filter_style');
document.getElementById('filter').addEventListener('input', function() {
if (!this.value) {
filterStyle.innerHTML = "";
return;
}
filterStyle.innerHTML = ".filterable:not([data-index*=\"" + this.value.toLowerCase() + "\"]) { display: none; }";
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment