Table of Contents

<HTMLElement>.hasClass()

Checks if an HTMLElement has a specific class attached.

Usage:
Boolean = Element.hasClass(className)
Member of:
Element, any valid HTMLElement.
Parameters:
className – the name of the class to check for, as String object or as quoted string (required).
Returns:
Booleantrue if the class is attached, false if it is not.
Notes:
  • This function is a simple wrapper for the classList.contains() function and only provides a slightly more convenient way of referencing this property.

Examples

Simple example:

let foo = document.getElementById('foo');
if (foo.hasClass('bar')) {};

See also

More information