2

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 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.