I have several classes called post-contenton div, and you have a search box whose event is fired on keyup. If there is no corresponding result, I would like to display "No matching results"
$('.post-content').each(function() {
$(this).attr('data-search-term', $(this).text().toLowerCase());
});
$('#search-criteria').on('keyup', function() {
var searchTerm = $(this).val().toLowerCase();
$('.post-content').each(function() {
if ($(this).filter('[data-search-term *= ' + searchTerm + ']').length > 0) {
$(this).show();
} else {
$(this).hide();
}
});
});
I tried to create an element inside an else block, but it seems to fire every time a key is pressed, thus printing the entire page with the event.
source
share