Categories
JavaScript Answers

How to add another class to a div with JavaScript?

Spread the love

Sometimes, we want to add another class to a div with JavaScript.

In this article, we’ll look at how to add another class to a div with JavaScript.

How to add another class to a div with JavaScript?

To add another class to a div with JavaScript, we call classList.add.

For instance, we write

document.getElementById("hello").classList.add("someClass");

to select the element with getElementById.

Then we call its classList.add method to add the someClass class to it.

Conclusion

To add another class to a div with JavaScript, we call classList.add.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *