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

177
Visualizações
Is it possible to put the array contents into a HTML class with Javascript

I'm learning Javascript and I'm trying to put the contents of the array into a html class.
I made an array like this: const terms = ["term1","term2"];

<figure id="8-12" class=" *Here the contents of the array* ">
   <a href="workshopPages/reeks/index.php">
   <img src="media/fotos/gallery/reeks.png" />
   <figcaption>CKV reeks</figcaption>
</figure>

Is there a way to put the contents of the array into the class?

Any help would be appreciated, thank you!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

you can use:

const terms = ["term1", "term2"];
let element = document.getElementById('8-12');
element.classList.add(...terms);

You can refer to this doc: https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

about 4 years ago · Juan Pablo Isaza Relatório

0

yes, by using .classList

const terms = ["term1","term2"];
var cl = document.querySelector('figure').classList;
cl.add.apply(cl, terms);

Also you can use .classList.add(... terms) as @alex197 answered, like:

const terms = ["term1","term2"];
document.querySelector('figure').classList.add(... terms);
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