Create a filter using Jquery

1

I'm doing a filter in jquery but I'm wrong in my logic somewhere and I can not seem to get out of the way. My filter is not fading with some words that I type, it brings me the whole block, even the typed word not being in the item. I'm having trouble solving it.

This is the portal: link

This is my script (just play on the page console 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';
        });
    }
});
    
asked by anonymous 22.08.2017 / 14:07

1 answer

0

Hello

I think it was a simple time to set the last object to display none

Test like this

 $("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';
        });

        var $elementsContent = $(".asset-full-content:contains(" + palavrasChave + ")");
        $elementsContent.each((index) => {
            $($elementsContent[index]).show();
            $($elementsContent[index]).find(".painel-faq").css( "display", "none");

            var itens = $($elementsContent[index]).find(".painel-faq .panel-title a:contains("+palavrasChave +")");
            itens.each((index) => {
               $(itens[index]).show();
$(itens[index]).parent().parent().parent().css("display","block")
            });

            $($elementsContent[index]).prev().show();  
            $($elementsContent[index]).style.display = 'none';

        });
    } else {
        var elements = $(".taglib-header");
        elements.show();
        elements.each((index) => {
            elements[index].nextElementSibling.style.display = 'none';
        });
      $('.painel-faq').show();  
    }
});
    
22.08.2017 / 15:43