To add a class to an element, you use the classList
property of the element. Suppose you have an element as follows:
<div>Item</div>
To add the box class to the <div>
element, you use the following:
const div = document.querySelector('div');
div.classList.add('box');
It’s possible to add multiple classes. The following adds three classes to the <div>
element:
const div = document.querySelector('div');
div.classList.add('box','primary','rounded');