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

85
Views
Add Class to div when a specific word appears a specific number of times on page

So, my aim is to add a Class name to a div but only when the word "premium" appears 10 times on the page.

My current code below adds a Class whenever the word "premium" appears. However, I need the Class name to only be added after 10 instances of the word.

.listing-package is the div where the word "premium" occurs and .submit-post is the div that I need to add a Class name to.

Any help would be greatly appreciated! Cheers :)

$(document).ready(function() {   
    document.querySelectorAll('.listing-package').forEach(el=>
    {
        if (/premium/.test(el.textContent) )
        { document.querySelector(".submit-post").classList.add("disabled") } 
    })
})
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

document.querySelectorAll gives a list of elements that contain a certain tag like a class or an id. Maybe you could use its length to know if it has 10 (or more) elements.

about 4 years ago · Juan Pablo Isaza Report

0

Change premium word check in if condition.

if ((el.textContent.match(/premium/g) || []).length >= 10)
about 4 years ago · Juan Pablo Isaza Report
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!