JavaScript NamedNodeMap removeNamedItem() Method

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

What You’ll Learn

The removeNamedItem() method of a NamedNodeMap removes an attribute by name and returns the removed Attr node. Learn the MDN element.attributes example, what happens when the attribute is missing, and how it compares with removeAttribute()—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

Removed Attr

03

Arg

attrName

04

Missing

Throws error

05

Use case

Drop attrs

06

Status

Baseline widely

Introduction

getNamedItem() reads an attribute from element.attributes. removeNamedItem() does the opposite—it deletes the attribute from the element and hands you back the Attr node that was removed.

MDN’s example removes a test attribute from a <pre> element, then checks with getNamedItem("test") to confirm it is gone.

💡
Beginner tip

For most everyday HTML tasks, call element.removeAttribute("class") instead. Use attributes.removeNamedItem("class") when you need the removed Attr node or are already walking the NamedNodeMap.

Understanding the removeNamedItem() Method

An instance method on NamedNodeMap that removes an attribute node by its name string.

  • attrName — the name of the attribute to remove (e.g. "test", "class").
  • Returns — the removed Attr node.
  • Exception — thrown when no attribute with that name exists (MDN).
  • Side effect — the attribute disappears from the element in the DOM.
  • Common sourceelement.attributes.removeNamedItem(name).
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

Call the method on a NamedNodeMap (usually from element.attributes):

JavaScript
namedNodeMap.removeNamedItem(attrName)

Parameters

  • attrName — string name of the attribute to remove.

Return value

The removed Attr node.

Exceptions

Thrown if there is no attribute with the given name.

Typical pattern (MDN idea)

JavaScript
const pre = document.querySelector("pre");
const attrMap = pre.attributes;

let result = `The 'test' attribute initially contains '${attrMap["test"].value}'.\n`;
result += "We remove it.\n\n";
attrMap.removeNamedItem("test");

result += attrMap.getNamedItem("test")
  ? "And 'test' still exists."
  : "And 'test' is no more to be found.";

pre.textContent = result;

⚡ Quick Reference

GoalCode / note
Remove by nameel.attributes.removeNamedItem("test")
Check firstgetNamedItem("test") before removing
Simpler HTML APIel.removeAttribute("test")
Missing nameThrows (MDN)
Return valueRemoved Attr node
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about NamedNodeMap.removeNamedItem().

Returns
Attr

Removed node

Arg
attrName

String name

Missing
throws

No silent fail

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN NamedNodeMap.removeNamedItem. Labs use live element.attributes on real DOM nodes.

📚 Getting Started

MDN pattern: remove an attribute and verify it is gone.

Example 1 — MDN Remove test Attribute

Read the value, remove it, then confirm with getNamedItem.

JavaScript
const pre = document.querySelector("pre");
const attrMap = pre.attributes;

let result = `The 'test' attribute initially contains '${attrMap["test"].value}'.\n`;
result += "We remove it.\n\n";
attrMap.removeNamedItem("test");

result += attrMap.getNamedItem("test")
  ? "And 'test' still exists."
  : "And 'test' is no more to be found.";

pre.textContent = result;
Try It Yourself

How It Works

MDN: removeNamedItem("test") drops the attribute from the live map and the element.

Example 2 — Capture the Returned Attr

The method returns the removed node—you can still read its properties.

JavaScript
const box = document.querySelector("#demo");
const removed = box.attributes.removeNamedItem("data-role");

console.log({
  name: removed.name,
  value: removed.value,
  stillOnElement: box.hasAttribute("data-role"),
});
Try It Yourself

How It Works

The Attr object survives removal; the element no longer exposes that attribute.

📈 Edge Cases & Comparisons

Map length, errors, and element-level alternatives.

Example 3 — length Decreases After Removal

Count attributes before and after removeNamedItem.

JavaScript
const box = document.querySelector("#demo");
const map = box.attributes;

const before = map.length;
map.removeNamedItem("class");
const after = map.length;

console.log(before, after, before - after);
Try It Yourself

How It Works

Each successful removal lowers map.length by one.

Example 4 — Throws When Attribute Is Missing

MDN documents an exception when the name does not exist.

JavaScript
const map = document.querySelector("#demo").attributes;

try {
  map.removeNamedItem("missing-attr");
} catch (err) {
  console.log(err.name); // NotFoundError
}
Try It Yourself

How It Works

Unlike removeAttribute (no-op when missing), removeNamedItem throws.

Example 5 — removeNamedItem() vs removeAttribute()

Map-level removal returns Attr; element method returns undefined.

JavaScript
const a = document.querySelector("#box-a");
const b = document.querySelector("#box-b");

const attrFromMap = a.attributes.removeNamedItem("title");
const fromElement = b.removeAttribute("title");

console.log(attrFromMap.name, typeof fromElement);
Try It Yourself

How It Works

Both remove the attribute from the element; only removeNamedItem gives you the Attr back.

🚀 Common Use Cases

  • Remove attributes while keeping the Attr node for inspection or reuse.
  • Clean up attributes when iterating element.attributes.
  • Teach the difference between map-level and element-level DOM APIs.
  • Interop with legacy code that mutates NamedNodeMap directly.
  • Mirror MDN’s attribute-removal demo on a live element.

🔧 How It Works

1

Get attributes map

Start from element.attributes—a live NamedNodeMap on the element.

Context
2

Call removeNamedItem(name)

Pass the attribute name string. Missing names throw per MDN.

Remove
3

Return removed Attr

The Attr leaves the map; map.length drops by one.

Result
4

Element updated

getNamedItem(name) returns null; hasAttribute(name) is false.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Throws when the attribute name is not found—unlike removeAttribute().
  • Returns the removed Attr, which you can still read after removal.
  • Prefer removeAttribute() for simple HTML attribute deletion.
  • Related learning: getNamedItem(), removeAttribute(), removeAttributeNode(), Element.attributes.

Universal Browser Support

NamedNodeMap.removeNamedItem() is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

NamedNodeMap.removeNamedItem()

Removes an attribute by name from the map and returns the removed Attr—or throws if not found.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported on NamedNodeMap (legacy DOM)
Legacy OK
NamedNodeMap.removeNamedItem() Excellent

Bottom line: Call map.removeNamedItem(attrName) on element.attributes when you need the removed Attr node back.

Conclusion

NamedNodeMap.removeNamedItem() removes an attribute by name and returns the removed Attr. Use it on element.attributes, handle missing names with try/catch, and prefer removeAttribute() when you only need to delete a value.

Continue with removeNamedItemNS(), removeAttribute(), getNamedItem(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Check with getNamedItem or hasAttribute first
  • Wrap in try/catch when the name might be missing
  • Use removeAttribute for everyday HTML cleanup
  • Store the returned Attr if you need name/value after removal
  • Remember map.length changes after removal

❌ Don’t

  • Call removeNamedItem on a name that may not exist without handling errors
  • Assume it returns null like getNamedItem
  • Remove attributes while forward-looping the same map without care
  • Confuse it with removeAttributeNode(attr) (takes Attr, not name)
  • Use it when removeAttribute is simpler and sufficient

Key Takeaways

Knowledge Unlocked

Five things to remember about removeNamedItem()

Remove attributes by name from NamedNodeMap.

5
Core concepts
⚙️02

Arg

attrName

Params
⚠️03

Missing

throws

MDN
📄04

length

decreases

Side effect
🎯05

Baseline

since Jul 2015

Status

❓ Frequently Asked Questions

It removes the Attr node with the given name from the map and returns that removed Attr. On elements, element.attributes.removeNamedItem('class') removes the class attribute from the element.
No. MDN marks NamedNodeMap.removeNamedItem() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
The removed Attr node. You can read its .name and .value after removal—the node still exists in memory even though it is no longer on the element.
MDN documents an exception: the method throws if there is no attribute with the given name (typically a NotFoundError DOMException).
removeNamedItem() works on the NamedNodeMap (element.attributes) and returns an Attr. element.removeAttribute(name) works on the element and returns undefined—it only removes the string value.
When you need the removed Attr node back, or when you are already working with element.attributes. For everyday HTML, removeAttribute() is usually simpler.
Did you know?

MDN’s removeNamedItem() demo prints “And ’test’ is no more to be found.” after removal—because getNamedItem("test") returns null once the attribute is gone.

Explore removeNamedItemNS()

Remove namespaced attributes by URI and localName.

removeNamedItemNS() method →

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