The getAttribute()
method of the Element
interface returns the value of a specified attribute on the element. If the given attribute does not exist, the value returned will either be null
or ""
(the empty string); see Non-existing attributes for details.
Syntax
let attribute = element.getAttribute(attributeName);
where
attribute
is a string containing the value ofattributeName
.attributeName
is the name of the attribute whose value you want to get.
Examples
<!-- example div in an html DOC --> <div id="div1">Hi Champ!</div> // in a console const div1 = document.getElementById('div1'); //=> <div id="div1">Hi Champ!</div> const exampleAttr= div1.getAttribute('id'); //=> "div1" cont align = div1.getAttribute('align') //=> null
Description
Lower casing
When called on an HTML element in a DOM flagged as an HTML document, getAttribute()
lower-cases its argument before proceeding.
Non-existing attributes
Essentially all web browsers (Firefox, Internet Explorer, recent versions of Opera, Safari, Konqueror, and iCab, as a non-exhaustive list) return null
when the specified attribute does not exist on the specified element; this is what the current DOM specification draft specifies. The old DOM 3 Core specification, on the other hand, says that the correct return value in this case is actually the empty string, and some DOM implementations implement this behavior. The implementation of getAttribute()
in XUL (Gecko) actually follows the DOM 3 Core specification and returns an empty string. Consequently, you should use element.hasAttribute()
to check for an attribute's existence prior to calling getAttribute()
if it is possible that the requested attribute does not exist on the specified element.
Retrieving nonce values
For security reasons, CSP nonces from non-script sources, such as CSS selectors, and .getAttribute("nonce")
calls are hidden.
let nonce = script.getAttribute('nonce');
// returns empty string
Instead of retrieving the nonce from the content attribute, use the nonce
property:
let nonce = script.nonce;
Specifications
Specification | Status | Comment |
---|---|---|
DOMThe definition of 'getAttribute()' in that specification. | Living Standard |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
getAttribute
|
Chrome
Full support 1 |
Edge
Full support 12 |
Firefox
Full support 23 |
IE
Full support 8 |
Opera
Full support 8 |
Safari
Full support 1.3 |
WebView Android
Full support 1 |
Chrome Android
Full support 18 |
Firefox Android
Full support 23 |
Opera Android
Full support 10.1 |
Safari iOS
Full support 1 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
Element.getAttribute() by Mozilla Contributors is licensed under CC-BY-SA 2.5.