How to add multiple classes to an Element using Javascript

To add multiple classes to an Element, we can use the classList.add() method. The classList.add() method allows us to pass one or more classes inside it. e.g.

myElement.classList.add('class1', 'class2');
  1. First, create an Element where we will add the classes.
    <div id="my-element"></div>
  2. Using the getElementById() method, select the <div> element and store in a variable.
    let myElement = document.getElementById('my-element');
  3. Now , pass multiple classes to the add() method to add the classes.
    myElement.classList.add('class1', 'class2');
  4. Output :-
    <div id="my-element" class="class1 class2"></div>