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



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sarout