JavaScript Element getAttributeNames() Method

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

What You’ll Learn

Element.getAttributeNames() is an instance method that returns every attribute name on an element as an array of strings. Learn how to pair it with getAttribute(), when you get an empty array, qualified namespace names, and five try-it labs aligned with MDN.

01

Kind

Instance method

02

Returns

string[]

03

Empty

[] if none

04

Pair with

getAttribute()

05

vs

.attributes

06

Status

Baseline widely

Introduction

Sometimes you need every attribute on an element—not just one. Older code often looped over element.attributes, a live NamedNodeMap of Attr nodes. That works, but MDN recommends getAttributeNames() plus getAttribute() as a lighter pattern.

The method takes no parameters and always returns an array. Zero attributes means [], not null.

💡
Beginner tip

getAttributeNames() gives you names only. To read values, loop and call el.getAttribute(name) for each name.

This page is part of JavaScript Element. It pairs naturally with getAttribute().

Understanding the getAttributeNames() Method

Calling el.getAttributeNames() snapshots the attribute names currently on the element. Values are not included—only qualified name strings.

  • It is an instance method on Element.
  • Returns an array of strings (empty when no attributes).
  • Prefixed namespace attributes appear as prefix:localName (e.g. xlink:href).
  • Namespaced attributes without a prefix return only the local name (MDN).
  • No parameters.
  • Memory-efficient alternative to iterating Element.attributes (MDN).

📝 Syntax

General form of Element.getAttributeNames (MDN):

JavaScript
getAttributeNames()

Parameters

None.

Return value

An array of strings containing attribute names. Empty array if the element has no attributes.

Common patterns

JavaScript
const el = document.getElementById("card");

// List names only
const names = el.getAttributeNames();
console.log(names); // ["id", "class", "data-id", …]

// Names + values (MDN pattern)
for (const name of el.getAttributeNames()) {
  console.log(name, el.getAttribute(name));
}

// Count attributes
console.log(el.getAttributeNames().length);

⚡ Quick Reference

GoalCode
All attribute namesel.getAttributeNames()
Loop names + valuesfor (const n of el.getAttributeNames()) getAttribute(n)
Count attributesel.getAttributeNames().length
No attributes[]
Check for a nameel.getAttributeNames().includes("data-id")
MDN statusBaseline Widely available (since October 2018)

🔍 At a Glance

Four facts to remember about Element.getAttributeNames().

Returns
string[]

Name strings only

Baseline
widely

Since Oct 2018

Empty
[]

Not null

Values
getAttr

Separate call

📋 getAttributeNames() vs attributes vs getAttribute()

getAttributeNames()element.attributesgetAttribute(name)
ReturnsArray of name stringsLive NamedNodeMap of Attr nodesOne value (string or null)
Includes valuesNoYes (on each Attr)Yes (one at a time)
PerformanceLightweight (MDN)Heavier Attr objectsSingle lookup
When empty[]Empty NamedNodeMapnull per missing name
Best forEnumerate all namesLegacy Attr node accessRead one attribute

Examples Gallery

Examples follow MDN Element.getAttributeNames() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

List names on a typical HTML element.

Example 1 — List Attribute Names

Read all names from an element with id, class, and data-*.

JavaScript
const card = document.getElementById("card");

console.log(card.getAttributeNames());
// ["id", "class", "data-id", "data-role"] (order may vary)
Try It Yourself

How It Works

You get an array of strings—one per attribute. Order is not guaranteed to match markup source order in all browsers.

Example 2 — Empty Array When No Attributes

A freshly created element with no attributes returns [].

JavaScript
const div = document.createElement("div");

console.log(div.getAttributeNames());
// []

console.log(div.getAttributeNames().length === 0);
// true
Try It Yourself

How It Works

MDN: if the element has no attributes, you always get an empty array—never null.

📈 Practical Patterns

Iterate with getAttribute, count, and namespace names.

Example 3 — Iterate Names and Values (MDN)

MDN’s recommended loop: names from getAttributeNames(), values from getAttribute().

JavaScript
const element = document.createElement("a");
element.setAttribute("href", "https://example.com");
element.setAttribute("target", "_blank");

for (const name of element.getAttributeNames()) {
  console.log(name, element.getAttribute(name));
}
// href https://example.com
// target _blank
Try It Yourself

How It Works

This pattern avoids holding every Attr node while still visiting all attributes on the element.

Example 4 — Count and Filter data-* Names

Find how many data- attributes exist on a component root.

JavaScript
const root = document.getElementById("widget");

const dataNames = root
  .getAttributeNames()
  .filter((name) => name.startsWith("data-"));

console.log(dataNames.length);
// 2

console.log(dataNames);
// ["data-id", "data-theme"]
Try It Yourself

How It Works

Because you get plain strings, array methods like filter and includes work naturally.

Example 5 — Qualified Namespace Names (MDN)

Prefixed attributes return prefix:localName; unprefixed namespaced attrs do not show the namespace.

JavaScript
const element = document.createElement("a");
element.setAttribute("href", "https://example.com");
element.setAttributeNS(
  "http://www.w3.org/1999/xlink",
  "xlink:href",
  "https://example.com"
);
element.setAttributeNS("http://www.w3.org/1999/xlink", "show", "new");

for (const name of element.getAttributeNames()) {
  console.log(name, element.getAttribute(name));
}
// href https://example.com
// xlink:href https://example.com
// show new
Try It Yourself

How It Works

MDN: xlink:href includes the prefix. The show attribute is in a namespace but has no prefix in the returned name—only show.

🚀 Common Use Cases

  • Serializing custom elements or widgets to JSON (names + getAttribute values).
  • Debugging which attributes are present on a node in devtools scripts.
  • Filtering data-* or aria-* attribute names.
  • Cloning or diffing attribute sets between two elements.
  • Replacing legacy for...of element.attributes loops with a lighter pattern.
  • Inspecting SVG/XML qualified attribute names.

🧠 How getAttributeNames() Builds the List

1

Call with no arguments

const names = el.getAttributeNames()

Invoke
2

Collect qualified names

Browser walks the element’s attribute list and builds name strings.

Collect
3

Return a new array

Fresh array each call—empty [] when there are no attributes.

Array
4

Read values separately

Loop names and call getAttribute(name) for each value.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available, October 2018).
  • Returns names only—not values.
  • Empty means [], not null.
  • Prefer with getAttribute() over heavy Attr iteration (MDN).
  • Namespace prefix rules differ for prefixed vs unprefixed namespaced attributes (MDN).
  • Related: getAttribute(), closest(), tagName, JavaScript hub.

Browser Support

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

Baseline Widely available

Element.getAttributeNames()

List every attribute name on an element as a string array.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop & Mobile
Yes
Apple Safari Supported · macOS & iOS
Yes
Opera Supported · Modern versions
Yes
Internet Explorer Not supported · Use attributes fallback
No
getAttributeNames() Excellent

Bottom line: Use getAttributeNames() to enumerate attribute names, then getAttribute() for values. Prefer this over attributes when you only need names and want a lighter loop.

Conclusion

Element.getAttributeNames() gives you every attribute name on an element in one array. Pair it with getAttribute() to read values efficiently, and remember [] when nothing is set.

Continue with getAttribute(), shadowRoot, closest(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Loop getAttributeNames() + getAttribute()
  • Expect [] for elements with no attributes
  • Use filter for data-* or aria-* names
  • Prefer this over attributes when names suffice
  • Handle qualified names like xlink:href in SVG

❌ Don’t

  • Expect values in the returned array
  • Assume null when there are no attributes
  • Rely on attribute order matching HTML source
  • Assume unprefixed names reveal XML namespaces
  • Mutate the array expecting DOM changes (re-call instead)

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.getAttributeNames()

All attribute names in one array.

5
Core concepts
📄 02

Empty

[]

None set
✍️ 03

Values

getAttribute

Pair
04

Baseline

widely available

Status
05

vs attrs

lighter

MDN

❓ Frequently Asked Questions

It returns an array of strings — the names of all attributes on the element. If there are no attributes, it returns an empty array.
No. MDN marks Element.getAttributeNames() as Baseline Widely available (across browsers since October 2018). It is not Deprecated, Experimental, or Non-standard.
An array of attribute name strings. Use getAttribute(name) in a loop to read each value.
getAttributeNames() returns only name strings. Element.attributes is a live NamedNodeMap of Attr nodes. MDN recommends getAttributeNames() with getAttribute() as a more memory-efficient alternative.
No. Call el.getAttributeNames() with no arguments.
If an attribute has a namespace prefix (e.g. xlink:href), the prefix is included in the returned name. Attributes in a namespace without a prefix return just the local name, with no namespace indication (MDN).
Did you know?

MDN recommends getAttributeNames() with getAttribute() as a memory-efficient alternative to looping element.attributes when you only need name/value pairs—not full Attr nodes.

More Element Topics

Browse Element methods and properties to keep building your DOM skills.

getAttribute() →

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.

8 people found this page helpful