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

134
Vistas
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 Respuestas
Responde la pregunta

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 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