DOM Element.getAttribute() Method
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
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.
Desktop





Tablets & Mobile





