$(this) being $('.live-search .col-md-6').each. As you may have guessed, I have multiples of the above code (being iterated via php). So how can I say that I want to hide the menu-item-category which is inside of the current menu-item-wrapper. Like I mentioned, I have multiple of these and I am doing a live search and I want to show only the menu-item-category which belongs to that search result.
@PICKAB00 - I tested it locally and it works for multiple instances - there must be something else going on to cause it to fail - either the structure is different than what I'm expecting or something when you handle a search
@REALRANDYALLEN - This works perfectly. Just one issue. I want to hide the rest of the category headings. But leave the currently search result related category.
I want to do this so I can put in to an inner div if the result is 0 "No result found" or something. Also to clean up the view as there are a lot of categories.
@PICKAB00 - I'm not sure I follow you, anything that has a search result the category header should be visible. By the way, looking at this a second time, this is a better structure:
$('.live-search-box').on('keyup', function () {
// reset everything
$('.live-search .col-md-6').show();
$('.menu-item-category').show();
var searchTerm = $(this).val().toLowerCase();
if (searchTerm.length < 1) return;
$('.live-search .col-md-6').each(function () {
if ($(this).filter('[data-search-term *= ' + searchTerm + ']').length == 0) {
$(this).hide();
$(this).parents(".menu-item-wrapper").find('.menu-item-category').hide();
}
});
});
@REALRANDYALLEN - The above does not display any results now. What I meant was, your previous code worked fine. But I wanted to remove all the category headers hide and show only the category header which belongs to the result. Like if the result is "green tea" and the header is "Tea's" then show only "Tea's" and remove all the rest. I mean keep the headers which belongs to the search results only. Hope this makes more sense. I am bad at explanations though it makes sense in my head.
@PICKAB00 - It does make sense, it's just odd because in my local demo that's exactly what's happening :) Perhaps our html is setup different? Maybe you should post an example of that as well with multiple categories as an example
@REALRANDYALLEN - Okay please don't hate me for this. And sorry for the wasted time. It was an issue on my end. I forgot just a tiny bit of class to be added. But here is the results I am getting with the corrected version just the same as the above. I mean your code (the one before the optimized one). And the HTML (the corrected version).
@REALRANDYALLEN - Actually there is an issue still. The above comment. I have attached an image too. The category header does not show up for some reason. But for the second one it does.
@REALRANDYALLEN - Here is the issue with some more detail. I do not know what is going on here. Here is the image of it without any input/searches. So this is how it looks like by default.
And lastly when I search "james" it messes everything. No header on the first search result and the second one has the header but it combines the rest of the stuff with the second header.
@PICKAB00 - Ok I see the issue, we can't hide the header without checking to see if any other siblings are apart of the search results, here's what I have that's working locally: