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