Advertisement

Google Ad Slot: content-top

Js Add and Remove Class

~1 min read · JS
On this page

The classList property offers methods like add, remove, toggle, and contains to work with classes efficiently.


Adding Classes:

The add() method adds one or more classes to an element.

Example
Existing text
Try it yourself

Removing Classes:

The remove() method removes one or more classes from an element.

Example
Existing text
Try it yourself

Toggling Classes:

The toggle() method adds a class if it is not present, and removes it if it is.

Example
Existing text
Try it yourself

Checking for Classes:

The contains() method checks if an element has a specific class.

Example
Existing text
Try it yourself