DOM DOMTokenList.contains() Method
The contains() method of a DOMTokenList, usually element.classList.contains(), checks whether an element has a class.
Description
The contains() method returns whether the list includes the given token, for example whether an element has a class. element.matches('.name') does the same check using a selector.
The check is case-sensitive, so contains('Active') doesn't match a class named active. Passing a string with spaces won't find multiple classes; check each one separately, or use matches('.a.b') to require both.
Before changing state based on a class, checking contains() lets you decide what to do next, though toggle()'s return value already tells you the new state when you're switching a class anyway.
Syntax
tokenList.contains(token)Parameters
- tokenThe token (class name) to look for.
Return Value
true if the list contains the token, otherwise false.
Example
Browser Support
The following information will show you the current browser support for the DOMTokenList.contains() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





