Favorites

DOM Element.hasAttribute() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Element.hasAttribute()
DOM Element MethodThis method is supported by all modern browsers.
Updated 28th November 2026

The hasAttribute() method checks whether an element has a given attribute.

Description

The hasAttribute() method returns whether the element has the named attribute at all, regardless of its value. It's the natural way to test boolean attributes.

The check is case-insensitive for HTML elements and ignores the value entirely, so hidden="false" still counts as present. To check whether an element has any attributes at all, use hasAttributes().

For attributes that have a matching boolean property, such as disabled, checked and open, reading the property is often simpler and reflects the element's current state, which can differ from the attribute for some form controls.

Syntax

element.hasAttribute(name)

Parameters

  • nameThe name of the attribute.

Return Value

true if the element has the attribute, otherwise false.

Example

<details open><summary>More</summary>Content</details>
const details = document.querySelector('details');
console.log(details.hasAttribute('open'));
console.log(details.hasAttribute('class'));

Browser Support

The following information will show you the current browser support for the Element.hasAttribute() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee