JavaScript NamedNodeMap removeNamedItemNS() Method

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

What You’ll Learn

The removeNamedItemNS() method of a NamedNodeMap removes a namespaced Attr node by namespace URI and localName. Learn the MDN XML DOMParser example, why the URI is not the prefix, and how it compares with removeNamedItem() and removeAttributeNS()—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

Removed Attr

03

Args

URI + local

04

Not prefix

namespace URI

05

Use case

XML / SVG

06

Status

Baseline widely

Introduction

getNamedItemNS() reads a namespaced attribute from element.attributes. removeNamedItemNS() is the mirror—it deletes that namespaced attribute and returns the removed Attr node.

MDN’s example parses XML with ob:one="test" and xmlns:ob="http://www.example.com/ob", then calls removeNamedItemNS("http://www.example.com/ob", "one") to drop the attribute.

💡
Beginner tip

MDN warns: the first argument is the namespace URI (the full URL), not the prefix (ob). Pass "http://www.example.com/ob", not "ob".

Understanding the removeNamedItemNS() Method

An instance method on NamedNodeMap that removes a namespaced attribute by URI and local name.

  • namespace — string URI of the attribute’s namespace (not the prefix).
  • localName — local part of the attribute name (without prefix).
  • Returns — the removed Attr node.
  • Exception — thrown when no matching namespaced attribute exists (MDN).
  • Common sourceelement.attributes on XML/SVG nodes.
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

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

JavaScript
namedNodeMap.removeNamedItemNS(namespace, localName)

Parameters

  • namespace — namespace URI string (not the xmlns prefix).
  • localName — the attribute’s local name without prefix.

Return value

The removed Attr for the namespace and local name.

Exceptions

Thrown if there is no attribute with the given namespace and local name.

Typical pattern (MDN idea)

JavaScript
const parser = new DOMParser();
const xmlString =
  '<warning ob:one="test" xmlns:ob="http://www.example.com/ob">Beware!</warning>';
const doc = parser.parseFromString(xmlString, "application/xml");

const warning = doc.querySelector("warning");
const attrMap = warning.attributes;

let result = `The 'ob:one' attribute initially contains '${attrMap["ob:one"].value}'.\n`;
result += "We remove it.\n\n";
attrMap.removeNamedItemNS("http://www.example.com/ob", "one");

result += attrMap["ob:one"]
  ? "And 'ob:one' still exists."
  : "And 'ob:one' is no more to be found.";

⚡ Quick Reference

GoalCode / note
Remove namespaced Attrel.attributes.removeNamedItemNS(uri, "one")
First argNamespace URI (not prefix)
Second arglocalName without prefix
Missing attrThrows (MDN)
Simpler APIremoveAttributeNS(uri, local)
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about NamedNodeMap.removeNamedItemNS().

Returns
Attr

Removed node

Args
uri, local

Two strings

URI
not prefix

MDN warning

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN NamedNodeMap.removeNamedItemNS. Labs use DOMParser and namespaced XML attributes.

📚 Getting Started

MDN XML pattern: remove ob:one and verify it is gone.

Example 1 — MDN Remove ob:one Attribute

Parse XML, remove the namespaced attribute, confirm removal.

JavaScript
const parser = new DOMParser();
const xmlString =
  '<warning ob:one="test" xmlns:ob="http://www.example.com/ob">Beware!</warning>';
const doc = parser.parseFromString(xmlString, "application/xml");

const warning = doc.querySelector("warning");
const attrMap = warning.attributes;

let result = `The 'ob:one' attribute initially contains '${attrMap["ob:one"].value}'.\n`;
result += "We remove it.\n\n";
attrMap.removeNamedItemNS("http://www.example.com/ob", "one");

result += attrMap["ob:one"]
  ? "And 'ob:one' still exists."
  : "And 'ob:one' is no more to be found.";

console.log(result);
Try It Yourself

How It Works

MDN: URI http://www.example.com/ob + localName one removes ob:one.

Example 2 — Capture the Returned Attr

The removed node still exposes namespace metadata.

JavaScript
const doc = new DOMParser().parseFromString(
  '<item app:id="42" xmlns:app="https://example.com/app"/>',
  "application/xml",
);
const item = doc.documentElement;
const removed = item.attributes.removeNamedItemNS("https://example.com/app", "id");

console.log({
  localName: removed.localName,
  value: removed.value,
  namespaceURI: removed.namespaceURI,
  stillOnElement: item.hasAttributeNS("https://example.com/app", "id"),
});
Try It Yourself

How It Works

You get the Attr back even though it no longer lives on the element.

📈 Edge Cases & Comparisons

Wrong URI, and alternatives for namespaced removal.

Example 3 — Throws When Namespace URI Is Wrong

Passing the prefix instead of the URI fails (MDN warning).

JavaScript
const doc = new DOMParser().parseFromString(
  '<warning ob:one="test" xmlns:ob="http://www.example.com/ob"/>',
  "application/xml",
);
const map = doc.documentElement.attributes;

try {
  map.removeNamedItemNS("ob", "one"); // prefix, not URI
} catch (err) {
  console.log(err.name); // NotFoundError
}
Try It Yourself

How It Works

MDN: namespace must be the URI http://www.example.com/ob, not "ob".

Example 4 — removeNamedItemNS() vs removeNamedItem()

Namespaced removal vs single-name removal on XML.

JavaScript
const doc = new DOMParser().parseFromString(
  '<warning ob:one="test" xmlns:ob="http://www.example.com/ob"/>',
  "application/xml",
);
const map = doc.documentElement.attributes;

const viaNS = map.removeNamedItemNS("http://www.example.com/ob", "one");
// removeNamedItem("ob:one") may work in some parsers but prefer removeNamedItemNS

console.log(viaNS.localName, map.length);
Try It Yourself

How It Works

For portable XML code, use removeNamedItemNS with URI + localName.

Example 5 — removeNamedItemNS() vs removeAttributeNS()

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

JavaScript
const doc = new DOMParser().parseFromString(
  '<a ob:one="A" xmlns:ob="http://www.example.com/ob"/>' +
  '<b ob:one="B" xmlns:ob="http://www.example.com/ob"/>',
  "application/xml",
);
const uri = "http://www.example.com/ob";
const a = doc.querySelector("a");
const b = doc.querySelector("b");

const attrFromMap = a.attributes.removeNamedItemNS(uri, "one");
const fromElement = b.removeAttributeNS(uri, "one");

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

How It Works

Both remove the namespaced attribute; only removeNamedItemNS gives you the Attr back.

🚀 Common Use Cases

  • Remove namespaced XML attributes from DOMParser documents.
  • Clean up SVG or MathML attributes while keeping the Attr node.
  • Interop with code that mutates element.attributes directly.
  • Teach namespace URI vs prefix when removing attributes.
  • Mirror MDN’s namespaced attribute removal demo.

🔧 How It Works

1

Parse or select element

Often an XML element from DOMParser with xmlns declarations.

Context
2

Call removeNamedItemNS(uri, local)

Pass namespace URI and localName—not the prefix from ob:one.

Remove
3

Return removed Attr

Attr leaves the map; map.length drops. Missing match throws per MDN.

Result
4

Attribute gone from element

getNamedItemNS(uri, local) returns null; hasAttributeNS is false.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • First parameter is namespace URI, not xmlns prefix.
  • Throws when no matching namespaced attribute exists.
  • Prefer removeAttributeNS() for simple namespaced deletion.
  • Related learning: getNamedItemNS(), removeNamedItem(), removeAttributeNS(), Element.attributes.

Universal Browser Support

NamedNodeMap.removeNamedItemNS() 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.removeNamedItemNS()

Removes the namespaced Attr for a namespace URI and localName—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.removeNamedItemNS() Excellent

Bottom line: Call map.removeNamedItemNS(namespaceURI, localName) on element.attributes for XML and SVG namespaced attribute removal.

Conclusion

NamedNodeMap.removeNamedItemNS() removes namespaced attributes by namespace URI and localName, returning the removed Attr. Use it on XML and SVG nodes, remember the URI is not the prefix, and prefer removeAttributeNS() when you only need to delete a value.

Continue with setNamedItem(), removeNamedItem(), removeAttributeNS(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Pass the namespace URI as the first argument
  • Use localName without the prefix
  • Wrap in try/catch when the attribute might be missing
  • Use getNamedItemNS first if you need to verify existence
  • Prefer removeAttributeNS for simple deletion

❌ Don’t

  • Pass the xmlns prefix where MDN expects a URI
  • Use removeNamedItemNS for ordinary HTML id / class
  • Assume it returns null like getNamedItemNS
  • Forget that missing namespaced attrs throw
  • Rely on removeNamedItem("ob:one") alone in portable XML

Key Takeaways

Knowledge Unlocked

Five things to remember about removeNamedItemNS()

Namespaced Attr removal by URI + localName.

5
Core concepts
⚙️02

Args

uri, local

Params
⚠️03

Not prefix

use URI

MDN
📄04

Missing

throws

Error
🎯05

Baseline

since Jul 2015

Status

❓ Frequently Asked Questions

It removes the Attr node matching a namespace URI and localName from the map and returns that removed Attr. On XML/SVG elements, element.attributes.removeNamedItemNS(uri, 'one') drops the namespaced attribute.
No. MDN marks NamedNodeMap.removeNamedItemNS() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
The namespace URI (a full URL string), not the xmlns prefix. MDN warns: namespace is the URI, not the prefix like ob in ob:one.
The removed Attr node. You can still read .localName, .value, and .namespaceURI after removal.
MDN documents an exception: the method throws if there is no attribute with the given namespace and local name (typically NotFoundError).
removeNamedItemNS() on attributes returns the removed Attr. element.removeAttributeNS(namespace, localName) works on the element and returns undefined.
Did you know?

MDN’s removeNamedItemNS() demo removes ob:one with removeNamedItemNS("http://www.example.com/ob", "one")—the URI and local name, not the ob prefix—then confirms attrMap["ob:one"] is gone.

Explore setNamedItem()

Insert or move Attr nodes after namespaced removal.

setNamedItem() 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