Favorites

DOM Element.getAttribute() Method

☕
Enjoying this site?

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

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

The getAttribute() method returns the value of an attribute on an element, or null if it isn't set.

Description

The getAttribute() method returns the value of a named attribute exactly as written in the HTML, or null if it's missing. That can differ from the matching property: for a link, getAttribute('href') returns what you wrote, while link.href returns the full resolved URL. For data-* attributes, the dataset property is more convenient.

Attribute names are case-insensitive for HTML elements, so getAttribute('ID') works the same as getAttribute('id'). Boolean attributes such as disabled return an empty string when present and null when absent, which is why hasAttribute() is clearer for them.

To list every attribute on an element, use getAttributeNames(), or loop over the attributes collection. ARIA attributes are also available as properties in modern browsers, such as el.ariaExpanded, but getAttribute() works everywhere.

Syntax

element.getAttribute(name)

Parameters

  • nameThe name of the attribute.

Return Value

The attribute's value as a string, or null if the element doesn't have it.

Example

<a href="/about.php" data-track="nav">About</a>
const link = document.querySelector('a');
console.log(link.getAttribute('href'));
console.log(link.href);
console.log(link.getAttribute('data-track'));
console.log(link.getAttribute('title'));

Browser Support

The following information will show you the current browser support for the Element.getAttribute() 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