Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!