Following on from a previous question which was solved here:
jQuery Page Search Filter – select paragraph text and ahref links
This above solution works perfectly, however, I now want to exclude the h4 headings from the search results. As can be seen in the Fiddle below, if any search term is inputted, the h4 heading (‘Research’) is always shown in the results:
https://jsfiddle.net/mfen723/zg6ej2w4/4/
The page I’m creating has many h4 headings, so I need to filter them out. The filter function I’m currently using is:
$(document).ready(function () {
$("#searchInput").on("keyup", function () {
var value = $(this).val().toLowerCase();
$("#research > p").filter(function () {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
Any help much appreciated.
2
Answers
You need to
return
the filtered item, jquery filter$("#research > p").filter
processes only<p>
elements, consider using$("#research > *").each
instead.You do not actually filter anything, since your function does not return anything, nor do you look at what the
.filter
function returns. Therefore.each
is more appropriate.