1
I’m making a filter in jquery but I’m getting my logic wrong somewhere and I’m not getting out of place. My filter is not disappearing with some words I type, it brings me the whole block, even though the word typed is not in the item. I’m having trouble solving.
This is the portal : http://www.enap.gov.br/web/pt-br/perguntas-frequentes
This is my script (just play on the console of the page to test)
$("input[name='_3_keywords']").keyup(function() {
var palavrasChave = $("input[name='_3_keywords']").val();
if (palavrasChave.trim() !== "") {
var content = document.querySelectorAll('.asset-full-content.show-asset-title');
content.forEach((element) => {
element.style.display = 'none';
});
$(".taglib-header").hide();
var $elements = $(".taglib-header:contains(" + palavrasChave + ")");
$elements.each((index) => {
$elements[index].nextElementSibling.style.display = 'block';
});
$elements.show();
var $elementsContent = $(".asset-full-content:contains(" + palavrasChave + ")");
$elementsContent.each((index) => {
$($elementsContent[index]).show();
console.log($($elementsContent[index]).prev());
$($elementsContent[index]).prev().show(); $elementContent.style.display = 'none';
});
} else {
var elements = $(".taglib-header");
elements.show();
elements.each((index) => {
elements[index].nextElementSibling.style.display = 'none';
});
}
});
Paulohdsouza , the problem is that even if I type "search" it brings me the whole block. in the case of "Research Block", in this block item 6 should not come in the search, but it comes along with the block, even if it has no word "search".
– Thiago lourenço
Do you want THE BLOCK to come with the items or just the items inside the block? You are filtering the items inside the block or the block?
– PauloHDSousa
My logic was to appear in the case, only the blocks with the items containing the typed word. If the block has 10 items , and only 2 has the word search , the rest would be on display:None. ai se o cara apagasse na busca , would return all.
– Thiago lourenço
You are filtering BLOCK TITLE, you want to filter BLOCK ITEMS? That’s it?
– PauloHDSousa
In fact it would have to be filtered both. The guy could search by the word of the title and the block.
– Thiago lourenço
Test and see if that’s it
– PauloHDSousa
It’s a little short, but I’ve done a lot of tests and you’re bugging out doing the research. Ex: if you do the research, he finds almost all , but in the last block - Casoteca de Gestão Publica, if you type "notes" he does not think. I still don’t understand why. And when doing the search and delete in the search field, the items do not return, are only the items you searched for, that is, it does not recover all items from the block after the search.
– Thiago lourenço
@Thiagolourenço editions that change code or the direction of the post are not recommended on this site, always choose to inform the author, even if the error is explicit.
– user28595