Table of Contents

<HTMLElement>.hasAttr()

Checks if an Element has a specific attribute.

Usage:
Boolean = Element.hasAttr(attrName)
Member of:
HTMLElement.
Parameter:
attrName – the name of the attribute to check for (required).
Returns:
true if the attribute exists, false, if not.
Notes:
  • This function can be used to check if empty attributes (like hidden, open, etc.) are present, which is not possible with the .getAttr() function.
  • This is just a simple wrapper for the JavaScript built-in hasAttribute method. If you don't need command chaining, it is more efficient to use .hasAttribute(…) instead.

Example

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

See also

More information