Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
Search data attribute of element with Jquery, with partial matches

I'm building a filter of items, using an input field and the on("keyup") event. It looks like this:

$("#inputFilter").on("keyup", filterPrograms);

It's working well to find items within a class, like this:

<h6 class="programName">Science</h6>

However, within some of those H6s, I've added a data attribute, like so:

<h6 class="programName" data-tag="indigenous interdisciplinary ">Aboriginal Studies</h6>

How can I modify the following code to filter both the class's text (currently working), and also the contents of the data-tag? This simply hides the parent block '.mix' whenever a partial match isn't true. Here's my function:

   function filterPrograms(event) {
        // Retrieve the input field text
        var filter = $('#inputFilter').val();
        // Loop through the blocks
        $(".programName").each(function(){
            // this part isn't working!!
            dataResult = $(this).is('[data-tag*='+filter+']') < 0;
            // If the item does not contain the text phrase, hide it
            textResult = $(this).text().search(new RegExp(filter, "i")) < 0;
            if (textResult || dataResult) {
                $(this).closest(".mix").hide();           
            } else {
                $(this).closest(".mix").show();
            }
        });
    }

Now, I'm pretty sure that it's because the .is() will never fully match, which is why I need a partial match. In the above example, typing "indi" should provide a positive result against the contents of the data-tag attribute; this doesn't work. Typing "abo" matches against textResult, and works fine.

I know I'm missing something, but reading the documentation (and SO) on this isn't helping. Thanks in advance.

Edit: here's the working function with @Triptych's solution:

$(".programName").each(function(){
    // If the item does not contain the text phrase hide it
    dataResult = $(this).is('[data-tag*="'+filter+'"]');
    textResult = $(this).text().search(new RegExp(filter, "i")) < 0;
    if (textResult && !dataResult) {
        $(this).closest(".mix").hide(); // Hide the item if there are no matches
    } else {
        $(this).closest(".mix").show(); // Show the item if there are matches
    }
});
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Well for one thing, you can't compare the result of .is() to 0 that way. is() returns a boolean.

So change this.

    dataResult = $(this).is('[data-tag*='+filter+']') < 0;

To this.

    dataResult = $(this).is('[data-tag*="'+filter+'"]');

Note that I also quoted the string for the attribute match, which will allow the query to include spaces.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda