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

138
Vistas
jQuery Applying class to list of THs via their text()

I have some ths that I know the text value of. I want to apply a class to a certain list of these ths.

What I have works like a charm, I just want to see if there's a better way to write it.

   //Custom extended jQuery psuedo function to check if text() of elem is exactly some string (ensures it doesn't overwrite existing fn)
//Ref: https://stackoverflow.com/a/18462522/2402616
        $.expr[':'].textEquals = $.expr[':'].textEquals ||
        $.expr.createPseudo(function(arg) {
            return function( elem ) {
                return $(elem).text() === arg;
            };
        });

     const textsOfThsToAddClassTo = ['foo','bar','fizz',buzz'];

    //Here's where my question really is
    textsOfThsToAddClassTo.forEach(text => {
        $(`th:textEquals('${text }')`).addClass('my-class');
    });

I'm just wondering if there's a better way to write that forEach part. Like, if I can use a selector to only contain what's in the array. Thanks!

about 4 years ago · Juan Pablo Isaza
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