Skip to content

Instantly share code, notes, and snippets.

@janboddez
Last active February 10, 2020 15:42
Show Gist options
  • Save janboddez/ee748993da8173c91db3c5222cae6cef to your computer and use it in GitHub Desktop.
Save janboddez/ee748993da8173c91db3c5222cae6cef to your computer and use it in GitHub Desktop.
$('#search').keyup(function() {
// Input field was changed (or typed into).
// If input contains any text.
if ($(this).val() != '') {
// Convert search term(s) to lowercase, split into words.
var searchTerms = $(this).val().toLowerCase().split(/\s+/);
// Traverse all results table rows. Well, except the header row.
$('#results tbody tr').each(function() {
var show = false;
// Traverse all paragraphs within.
$(this).find('p').each(function() {
var text = $(this).text().toLowerCase();
var i;
// For each and every search term.
for (i = 0; i < searchTerms.length; ++i) {
// If `text` contains a keyword.
if (text.indexOf(searchTerms[i]) != -1) {
// We'll want to show this row.
show = true;
}
}
});
if (show) {
$(this).show();
} else {
// No keywords found in any of this row's paragraphs.
$(this).hide();
}
});
} else {
// While input field is empty, show everything.
$('#results tr').show();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment