Skip to content

Instantly share code, notes, and snippets.

@sublimecoder
Last active August 29, 2015 14:00
Show Gist options
  • Save sublimecoder/11189419 to your computer and use it in GitHub Desktop.
Save sublimecoder/11189419 to your computer and use it in GitHub Desktop.
Searches through the content of a set of divs, and hides the ones that do not match the search.
function disableEnterKey(e)
{
var key;
if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
return (key != 13);
}
//Filter for the table. You feed it the object and the ID to get it to respond.
function filterDivs (phrase, _cls){
var words = phrase.value.toLowerCase().split(" ");
var divs = document.getElementsByClassName(_cls);
var ele;
for (var r = 1; r < divs.length; r++){
ele = divs[r].innerHTML.replace(/<[^>]+>/g,"");
var displayStyle = 'none';
for (var i = 0; i < words.length; i++) {
if (ele.toLowerCase().indexOf(words[i])>=0)
displayStyle = '';
else {
displayStyle = 'none';
break;
}
}
divs[r].style.display = displayStyle;
}
}
// search files is the input we are binding to
// contact_outer is the class of div we are filtering through.
// change these as needed.
// jquery is required for the section below. This can be rewritten to use plain javascript.
$(function(){
$(".Searchfield").keyUp(filterDivs(this, 'contact_outer'))
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment