element.getAttribute() Get an attribute
Last updated: 2026-09-09
element.getAttribute() Get an attribute
getAttribute returns the value of an HTML attribute, or null.
| Category | DOM |
|---|---|
| ES version | DOM |
📝 Syntax
const val = el.getAttribute('href');
⚙️ Parameters
| name | The attribute name. |
|---|
Returns:The attribute value or null.
Mutates the original:No (returns a new value)
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
(attribute value or null)
💡 Tip Use el.dataset.id for data-* attributes — cleaner than getAttribute.
ℹ️ info getAttribute returns the raw HTML attribute value.
🌐 Browser support
| Browser | |||||
|---|---|---|---|---|---|
| element.getAttribute() | ✓ v1 | ✓ v12 | ✓ v1 | ✓ v1 | ✓ v1 |
| Supported by all modern browsers | |||||
🏷️ Related Items
💡 Related Cases
More case studies coming soon — check back later.
📚 Related Tutorials
❓ FAQ
QWhat does null from getAttribute mean?
AThe attribute does not exist on the element.
QgetAttribute vs el.href?
AgetAttribute gives raw value; el.href gives absolute URL.
QHow to get data-* attributes?
Ael.dataset.id or el.getAttribute('data-id').