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