Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

220
Vistas
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
    }
});
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda