JavaScript NamedNodeMap removeNamedItem() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

The removeNamedItem() method of a NamedNodeMap removes the Attr for a given name and returns that removed node (MDN). You usually call it on element.attributes.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call map.removeNamedItem. Start with this line.

JavaScript
namedNodeMap.removeNamedItem(attrName)
CallWhat it does
namedNodeMap.removeNamedItem(attrName)Primary signature.
map.removeNamedItem(...)Call with the arguments your case needs.

Built into the browser. Call removeNamedItem(...) on a NamedNodeMap — no import required.

Library declarations
JavaScript
namedNodeMap.removeNamedItem(attrName)

Parameters

ParameterDescription
attrNameRequired. string. Attribute name to remove (e.g. "test", "class")
Missing nameRequired. NotFoundError. Throws — does not fail silently (MDN)

Return Value

Return ValueDescription
*The removed node (still readable in memory)

JavaScript Examples

Example 1 — MDN Sample

Remove a present attribute, then confirm with getNamedItem.

JavaScript
const pre = document.querySelector("pre"); // <pre test="testValue">
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.";

console.log(result);
Try It Yourself

How It Works

  1. Get the map. pre.attributes is a live NamedNodeMap.
  2. Read first. attrMap["test"].value shows the starting value.
  3. Remove by name. removeNamedItem("test") drops the Attr from the element (MDN).
  4. Verify. getNamedItem("test") is now null.

Example 2 — Capture the Returned Attr

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

JavaScript
const box = document.getElementById("demo"); // data-role="panel"
const removed = box.attributes.removeNamedItem("data-role");

console.log(removed.name);                    // "data-role"
console.log(removed.value);                   // "panel"
console.log(box.hasAttribute("data-role"));   // false
Try It Yourself

How It Works

  1. Call removeNamedItem. Pass the attribute name string.
  2. Keep the return value. You get an Attr object back.
  3. Read fields. .name and .value still describe that attribute.
  4. Check the element. hasAttribute is false — it is no longer on the DOM node.

Example 3 — Missing Name Throws

MDN: no attribute with that name → NotFoundError.

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

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

// Safer everyday alternative when missing is OK:
document.getElementById("demo").removeAttribute("missing-attr");
Try It Yourself

How It Works

  1. Ask for a name that is not there. The map has no missing-attr.
  2. Call removeNamedItem. The browser throws a DOMException.
  3. Catch the error. err.name is typically "NotFoundError".
  4. Prefer removeAttribute for soft deletes. It is a no-op when the name is missing — see removeAttribute().

Browser Support

NamedNodeMap.removeNamedItem() is Baseline Widely available (MDN: across browsers since July 2015). Safe to use without a polyfill in modern teaching and production code.

✓ Baseline · Widely available

removeNamedItem()

Removes the Attr for a given name from a NamedNodeMap and returns it. Available on element.attributes in all major browsers.

98% Widely available
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
NamedNodeMap.removeNamedItem() Excellent

Bottom line: Use freely in modern apps. Prefer removeAttribute() for everyday removals; use removeNamedItem() when you need the Attr node or are already on the map.

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 still read its .name and .value after removal — the node exists in memory even though it is no longer on the element.
MDN: the method throws a NotFoundError DOMException when there is no attribute with the given name.
removeNamedItem() works on the NamedNodeMap and returns an Attr. element.removeAttribute(name) works on the element, returns undefined, and does not throw when the name is missing.
When you need the removed Attr node back, or when you are already working with element.attributes. For everyday HTML, removeAttribute() is usually simpler.

Next: removeNamedItemNS()

Learn how to remove namespaced Attr nodes with a namespace URI and local name.

removeNamedItemNS() tutorial →

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.

9 people found this page helpful