Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda