Skip to content

Instantly share code, notes, and snippets.

@joshstrange
Created April 28, 2014 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshstrange/11383566 to your computer and use it in GitHub Desktop.
Save joshstrange/11383566 to your computer and use it in GitHub Desktop.
A very simple full text search for hiding/showing elements on a page that requires jQuery
var QuickAndDirtySearch = new QuickAndDirtySearchController($('#myTable tbody tr'), $('#searchBox'));
function QuickAndDirtySearchController(elements, searchBox) {
var instance = this;
instance.elements = elements;
instance.searchBox = searchBox;
instance.elements.each(function(){
var el = $(this);
var text = instance.recursiveText(el);
el.data('searchText', text);
});
instance.searchBox.on('keyup', function(){
var val = $(this).val();
instance.search(val);
});
};
QuickAndDirtySearchController.prototype.search = function(text) {
var instance = this;
instance.elements.each(function(){
var el = $(this);
if(el.data('searchText').indexOf(text.toLowerCase()) != -1) {
el.show();
} else {
el.hide();
}
});
};
QuickAndDirtySearchController.prototype.recursiveText = function(el) {
var instance = this;
var fullText =[];
var children = el.children();
if(children.length) {
children.each(function(){
fullText.push(instance.recursiveText($(this)));
});
return fullText.join(' ');
} else {
return el.text().toLowerCase();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment