add class to element in javascript

To add a class to an element in Javascript, you can use the classList property of the element.

index.tsx
// Select the element
const element = document.querySelector('#my-element');

// Add the class
element.classList.add('my-class');
130 chars
6 lines

In this example, we first select the element using document.querySelector(), and then use the classList.add() method to add the class my-class to the element. You can replace my-class with the name of the class you wish to add.

gistlibby LogSnag