Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Created July 23, 2013 09:22
Show Gist options
  • Save kezabelle/6061118 to your computer and use it in GitHub Desktop.
Save kezabelle/6061118 to your computer and use it in GitHub Desktop.
Quick and dirty filtering in JavaScript ... inefficient!
var previous_result = '';
// listen to an input ...
$('#filter-input').live('keyup', function(evt) {
var filtering_by = $(this).val().toLowerCase();
if (previous_result === filtering_by) {
return;
}
previous_result = filtering_by;
// eg: labels, list items, whatever.
var possibles = $('#nodes-to-filter');
possibles.each(function(evt) {
var $this = $(this);
var text = $this.text().toLowerCase();
// hide or show the parent element.
if (text.indexOf(filtering_by) !== -1) {
$this.parent().show();
} else {
$this.parent().hide();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment