Last active
February 10, 2020 15:42
-
-
Save janboddez/ee748993da8173c91db3c5222cae6cef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#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