JavaScript Element attributes Property

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance property

What You’ll Learn

Element.attributes is a read-only instance property that returns a live NamedNodeMap of all attribute nodes on an element. Learn how to list names and values, how it differs from an Array, and when to use helpers like hasAttributes()—with five examples and try-it labs.

01

Kind

Instance property

02

Access

Read-only

03

Type

NamedNodeMap

04

Items

Attr nodes

05

Status

Baseline widely

06

Live?

Yes

Introduction

HTML attributes like id, class, and contenteditable store extra information on a tag. In JavaScript, each of those becomes an Attr node.

element.attributes gives you a live map of every attribute currently registered on that element—useful for debugging, cloning metadata, or walking all names at once.

JavaScript
const paragraph = document.querySelector("p");
const attributes = paragraph.attributes;
console.log(attributes.length);
💡
Beginner tip

Prefer getAttribute() / setAttribute() when you know the name. Use attributes when you need to inspect all of them.

Understanding the Property

MDN: the Element.attributes property returns a live collection of all attribute nodes registered to the specified node. It is a NamedNodeMap, not an Array, so it has no Array methods, and Attr indexes may differ among browsers.

  • Read-only property — you read the map; mutate attributes via DOM APIs.
  • NamedNodeMap — key/value style access to Attr nodes.
  • Live — updates when attributes are added or removed.
  • Baseline — widely available since July 2015 (MDN).

📝 Syntax

JavaScript
attributes

Value

A NamedNodeMap object containing the element’s Attr nodes.

ItemDetail
TypeNamedNodeMap
AccessRead-only property (map contents change via attribute APIs)
Iteratefor (const attr of el.attributes)
HelpershasAttributes(), getNamedItem(), getAttribute()
⚠️
Not an Array

Do not call .map() or .forEach() on attributes directly. Convert with Array.from(el.attributes) when you need array methods.

📋 MDN Enumeration Example Shape

MDN enumerates attributes on a paragraph with id, class, and contenteditable, then prints each name and value:

JavaScript
<p id="paragraph" class="green" contenteditable>Sample Paragraph</p>
JavaScript
const paragraph = document.getElementById("paragraph");

if (paragraph.hasAttributes()) {
  for (const attr of paragraph.attributes) {
    console.log(`${attr.name} -> ${attr.value}`);
  }
}

Related helpers: getAttribute(), setAttribute(), removeAttribute(), and hasAttribute().

⚡ Quick Reference

GoalCode / note
Get mapel.attributes
Countel.attributes.length
Loopfor (const attr of el.attributes)
By nameel.attributes.getNamedItem("id")
To arrayArray.from(el.attributes)
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.attributes.

Kind
get only

Instance

Type
NamedNodeMap

Not Array

Live
yes

Auto updates

Baseline
widely

Jul 2015+

Examples Gallery

Examples follow MDN Element: attributes. Labs use a simple paragraph so you can inspect the live map safely.

📚 Getting Started

Read the map and enumerate names like MDN.

Example 1 — Read attributes

Log the length of the attribute collection on a paragraph.

JavaScript
const paragraph = document.querySelector("p");
const attributes = paragraph.attributes;
console.log(attributes.length);
console.log(attributes.constructor.name);
Try It Yourself

How It Works

For <p id="paragraph" class="green" contenteditable>, browsers expose three attribute nodes in the map (boolean attributes still count).

Example 2 — Enumerate With for...of

MDN pattern: print each attribute name and value.

JavaScript
const paragraph = document.getElementById("paragraph");
let output = "";

if (paragraph.hasAttributes()) {
  for (const attr of paragraph.attributes) {
    output += `${attr.name} -> ${attr.value}\n`;
  }
}
console.log(output);
Try It Yourself

How It Works

Each item is an Attr with name and value. Order is not guaranteed across browsers, so do not rely on index positions.

📈 Lookup, Live Updates & Snapshot

Look up by name, prove the map is live, and feature-detect.

Example 3 — getNamedItem

Fetch one Attr node by name from the map.

JavaScript
const el = document.getElementById("paragraph");
const idAttr = el.attributes.getNamedItem("id");
console.log({
  name: idAttr.name,
  value: idAttr.value,
  viaGetAttribute: el.getAttribute("id")
});
Try It Yourself

How It Works

getNamedItem returns the Attr node. getAttribute returns only the string value—usually clearer for everyday code.

Example 4 — Live Collection

Adding an attribute updates the same NamedNodeMap object.

JavaScript
const el = document.createElement("div");
const map = el.attributes;
console.log(map.length); // 0

el.setAttribute("data-role", "card");
console.log(map.length); // 1
console.log(map.getNamedItem("data-role").value);
Try It Yourself

How It Works

You do not need to re-read el.attributes. The map stays linked to the element.

Example 5 — Support Snapshot

Feature-detect and remember the Array conversion tip.

JavaScript
console.log({
  supported: "attributes" in Element.prototype,
  type: "NamedNodeMap (not Array)",
  tip: "Use for...of or Array.from(el.attributes)",
  status: "Baseline Widely available (MDN)"
});
Try It Yourself

How It Works

Treat attributes as a specialized live map. Convert only when array helpers help.

🚀 Common Use Cases

  • Debugging: dump every attribute name and value on a node.
  • Cloning or serializing metadata without hard-coding names.
  • Teaching DOM: show the difference between attributes and properties.
  • Checking whether an element currently has any attributes.
  • Building small inspectors or accessibility audit helpers.

🔧 How It Works

1

HTML declares attributes

id, class, data-*, and more.

Markup
2

Browser builds Attr nodes

Each attribute becomes an Attr with name and value.

Nodes
3

attributes exposes the map

A live NamedNodeMap you can loop or look up by name.

Read
4

Changes stay in sync

setAttribute / removeAttribute update the same map.

📝 Notes

Browser Support

Element.attributes is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.attributes

Read-only — live NamedNodeMap of Attr nodes on the element.

Baseline Widely available
Google Chrome Supported
Yes
Microsoft Edge Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Opera Supported
Yes
Internet Explorer Supported (legacy NamedNodeMap)
Yes
attributes Baseline

Bottom line: Use attributes to inspect all Attr nodes. Prefer getAttribute for a known name. Iterate with for...of, or convert with Array.from when you need array methods.

Conclusion

attributes is your live window into every attribute on an element. Loop with for...of, look up with getNamedItem, and remember it is a NamedNodeMap—not an array.

Continue with childElementCount, assignedSlot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use for...of to walk all attributes
  • Call hasAttributes() before dumping
  • Prefer getAttribute for a known name
  • Convert with Array.from only when needed
  • Treat the map as live when caching a reference

❌ Don’t

  • Call Array methods on attributes directly
  • Depend on a fixed attribute index order
  • Confuse HTML attributes with JS object properties
  • Overwrite el.attributes = ... (read-only)
  • Skip null checks after getNamedItem

Key Takeaways

Knowledge Unlocked

Five things to remember about attributes

Live NamedNodeMap of Attr nodes—inspect every name and value.

5
Core concepts
📝 02

NamedNodeMap

not Array

Type
🔍 03

Attr list

name + value

Use
04

Baseline

widely available

Status
🎯 05

for...of

best loop

Tip

❓ Frequently Asked Questions

It returns a live NamedNodeMap of all Attr nodes registered on the element — each entry has a name and value.
No. MDN marks Element.attributes as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
No. It is a NamedNodeMap, not an Array. It has no Array methods like map or forEach. Use for...of, or Array.from(el.attributes) when you need array helpers.
Yes. If you add or remove attributes later, the same NamedNodeMap object reflects those changes.
Call el.hasAttributes(), or check el.attributes.length > 0.
Use el.getAttribute("id"), or el.attributes.getNamedItem("id") to get the Attr node.
Did you know?

Boolean attributes like contenteditable still appear in attributes. Their value may be an empty string even though the attribute is present.

Next: childElementCount

Learn how to count an element’s child tags from JavaScript.

childElementCount →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful