JavaScript NamedNodeMap removeNamedItemNS() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

NamedNodeMap.removeNamedItemNS(namespace, localName) removes the namespaced Attr from the map (usually element.attributes) and returns it (MDN).

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

Syntax

Call map.removeNamedItemNS. Start with this line.

JavaScript
removeNamedItemNS(namespace, localName)
CallWhat it does
removeNamedItemNS(namespace, localName)Primary signature.
map.removeNamedItemNS(...)Call with the arguments your case needs.

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

Library declarations
JavaScript
removeNamedItemNS(namespace, localName)

Parameters

ParameterDescription
namespaceRequired. Namespace URI (not the prefix)
localNameRequired. Local name of the attribute

Return Value

Return ValueDescription
*Removed Attr

JavaScript Examples

Example 1 — MDN Remove

Remove ob:one using the namespace URI.

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

console.log(map["ob:one"].value); // test
map.removeNamedItemNS("http://www.example.com/ob", "one");
console.log(!!map["ob:one"]);     // false
Try It Yourself

How It Works

  1. Parse. XML has ob:one="test" and an xmlns:ob URI.
  2. Match by URI. First argument is the full namespace URL.
  3. Local name. Second argument is "one", not "ob:one".
  4. Gone. Looking up ob:one afterward fails.

Example 2 — Returned Attr

The method returns the removed Attr so you can still read it.

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

console.log(removed.localName);     // id
console.log(removed.value);         // 42
console.log(removed.namespaceURI);  // https://example.com/app
Try It Yourself

How It Works

  1. Remove. Capture the return value in removed.
  2. Local name. Still "id" after detachment.
  3. Value. The string value remains on the Attr node.
  4. URI. namespaceURI still identifies the namespace.

Example 3 — Length Drops

Removing a namespaced Attr shrinks the map.

JavaScript
const doc = new DOMParser().parseFromString(
  '<n a:x="1" xmlns:a="https://example.com/a"/>',
  "application/xml"
);
const map = doc.documentElement.attributes;

console.log(map.length); // includes xmlns + a:x
map.removeNamedItemNS("https://example.com/a", "x");
console.log(map.length); // one less
Try It Yourself

How It Works

  1. Count first. The map holds the xmlns declaration and a:x.
  2. Remove x. Match URI + local name.
  3. Length falls. One Attr is gone from the map.
  4. Tip. See also length.

Example 4 — NotFoundError

Missing attributes (or a prefix instead of a URI) throw.

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

try {
  map.removeNamedItemNS("ob", "one"); // prefix — wrong
} catch (e) {
  console.log(e.name); // NotFoundError
}

try {
  map.removeNamedItemNS("http://www.example.com/ob", "missing");
} catch (e) {
  console.log(e.name); // NotFoundError
}
Try It Yourself

How It Works

  1. Prefix trap. "ob" is not the namespace URI.
  2. Missing name. A local name that is not present also fails.
  3. Exception. Both cases throw NotFoundError.
  4. Guard. Use try/catch, or check with getNamedItemNS() first.

Example 5 — vs removeAttributeNS

Map method returns Attr; element method returns undefined.

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

const fromMap = a.attributes.removeNamedItemNS(uri, "one");
const fromEl = b.removeAttributeNS(uri, "one");

console.log(fromMap.localName); // one
console.log(typeof fromEl);     // undefined
Try It Yourself

How It Works

  1. Map API. removeNamedItemNS hands back the Attr.
  2. Element API. removeAttributeNS only deletes; return is undefined.
  3. Same URI rules. Both need namespace URI + local name.
  4. Choose. Prefer the map when you need to move or inspect the Attr.

Browser Support

NamedNodeMap.removeNamedItemNS() is marked Baseline Widely available on MDN (since July 2015). DOM API — not a Node.js built-in.

✓ Baseline · Widely available

removeNamedItemNS()

Remove namespaced Attr nodes across modern browsers. Pass the namespace URI, not the prefix.

98% Core tag support
Chrome 1+
Full support
Edge 12+
Full support
Firefox 1+
Full support
Safari 1+
Full support
Opera ✓
Full support
HTML element 100% supported

Bottom line: Safe for production. Use removeNamedItemNS(uri, localName) when you need the removed Attr; use removeAttributeNS when you only need to delete.

Frequently Asked Questions

It removes the Attr matching a namespace URI and localName from the map and returns that Attr. Typical use: element.attributes.removeNamedItemNS(uri, "one") on XML or SVG attributes.
No. MDN marks NamedNodeMap.removeNamedItemNS() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
The namespace URI (full URL string), not the xmlns prefix. MDN warns: namespace is the URI, not the prefix (for example use http://www.example.com/ob, not ob).
The removed Attr node. You can still read localName, value, and namespaceURI after removal.
It throws NotFoundError (DOMException) when no attribute matches the given namespace and local name.
removeNamedItemNS on attributes returns the removed Attr. element.removeAttributeNS(namespace, localName) returns undefined.

Next: setNamedItem()

Insert or replace an Attr on a NamedNodeMap.

setNamedItem() →

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