Prefer getAttribute when the name is dynamic or not only data-*.
Compatibility
Browser Support
Element.getAttribute() is Baseline Widely available on MDN (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
✓ Baseline Widely available
Element.getAttribute()
Read HTML attribute values as strings (or null). Core DOM — available everywhere modern.
2015+Widely available
Google ChromeAll modern
Supported
Mozilla FirefoxAll modern
Supported
Apple SafariAll modern
Supported
Microsoft EdgeAll modern
Supported
OperaAll modern
Supported
Internet ExplorerSupported
Supported
getAttribute()Baseline
Bottom line: Safe for all modern browsers and IE. Always null-check missing attributes.
Frequently Asked Questions
It returns the value of a named HTML attribute on the element as a string (MDN). If the attribute does not exist, it returns null.
No. MDN marks Element.getAttribute() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
null. Always check for null before using the value, or use hasAttribute() first.
IDL properties like id reflect specific attributes and may have different types or defaults. getAttribute() always returns a string or null for any attribute name you pass.
You can use getAttribute("data-id") or the dataset API (el.dataset.id). dataset is often cleaner for data-* names; getAttribute works for any attribute.
For CSP security, nonce values are hidden from getAttribute on script elements (MDN). Use the nonce property instead: script.nonce.