Categories
JavaScript Answers

How to add/remove several classes in one single instruction with classList and JavaScript?

Spread the love

To add/remove several classes in one single instruction with classList and JavaScript, we can call add or remove with all the classes we want to add or remove.

For instance, we write

elem.classList.add("first", "second", "third");

to call elem.classList.add to add the first, second, and third classes.

Likewise, we write

elem.classList.remove("first", "second", "third");

to call elem.classList.remove to remove the first, second, and third classes.

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 *