Is it possible to put the array contents into a HTML class with Javascript

Viewed 65

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!

2 Answers

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);
Related