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

133
Views
Target elements in Dom matching with Classes present in Array

I have a Array of classes, its a dynamic Array which can have n number of class names like this.

let arr = ['Product', 'year', 'role'];

and then I have lots of elements with these classes inside a parent div like this.

<div class="parent">
    <div class="year"></div>
    <div class="abc"></div>
    <div class="Product"></div>
    <div class="diffClass"></div>
    <div class="role"></div>
    <div></div>
</div>

Now in this structure I need to add class active on page load only on the div's which's class present in the Array. I tried to map the array but no table to get the divs with matching with Arrays class Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can select "or" classes using ,, eg

$(".product,.year,.role")

will select if any of them match
(conversely, to make them all match, use .product.year.role - that's not the requirement here)

To convert your array to a string, use arr.join, giving:

let arr = ['Product', 'year', 'role'];

$("." + arr.join(",.")).addClass("active");
.active { color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="parent">
    <div class="year">year</div>
    <div class="abc">abc</div>
    <div class="Product">Product</div>
    <div class="diffClass">diffClass</div>
    <div class="role">role</div>
    <div></div>
</div>

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!