The classList property provides a convenient way to manage an element’s class attributes in JavaScript. It offers methods for adding, removing, toggling, and replacing classes.

Key Points:

  • Live Collection: classList represents a live collection of classes on an element. Changes made using these methods are reflected in the element’s class attribute.
  • Method Options:
    • add(className): Adds a class to the element’s class list.
    • remove(className): Removes a class from the element’s class list.
    • toggle(className, force): Toggles a class (add if absent, remove if present). Optional force argument ensures adding the class even if already present.
    • replace(oldClassName, newClassName): Replaces an existing class with a new class.