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

176
Views
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 answers
Answer question

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 Report

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