JavaScript Element getAttributeNodeNS() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

The Element.getAttributeNodeNS() method returns a namespaced attribute as an Attr node (MDN). Pass a namespace URI and a local name. Missing match → null. For the string only, use getAttributeNS().

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

Syntax

Call el.getAttributeNodeNS. Start with this line.

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

Built into the browser. Call getAttributeNodeNS(...) on an Element — no import required.

Library declarations
JavaScript
el.getAttributeNodeNS(namespace, localName)

Parameters

ParameterDescription
namespaceRequired. Namespace URI, or null for none
localNameRequired. Local attribute name (no prefix)

Return Value

Return ValueDescription
*Attr node, or null

JavaScript Examples

Example 1 — Read Namespaced Attr

Create an XLink href, then get its Attr node.

JavaScript
const el = document.getElementById("box");
const out = document.getElementById("out");
const XLINK = "http://www.w3.org/1999/xlink";

el.setAttributeNS(XLINK, "xlink:href", "https://example.com");
const attr = el.getAttributeNodeNS(XLINK, "href");
out.textContent = attr.value;
Try It Yourself

How It Works

  1. Set with NS. setAttributeNS stores the namespaced attribute.
  2. Pass URI + local name. Lookup uses "href", not "xlink:href".
  3. Get an Attr. getAttributeNodeNS returns the node.
  4. Read .value. Same string you would get from getAttributeNS.

Example 2 — Missing → null

Wrong namespace or local name means no match.

JavaScript
const el = document.getElementById("box");
const out = document.getElementById("out");
const XLINK = "http://www.w3.org/1999/xlink";

el.setAttributeNS(XLINK, "xlink:href", "https://example.com");
const attr = el.getAttributeNodeNS(XLINK, "title");
out.textContent = attr === null ? "null" : attr.value;
Try It Yourself

How It Works

  1. Attribute exists as href. Local name is not title.
  2. Lookup fails. Namespace + local name must both match.
  3. Result is null. Same rule as other attribute getters.
  4. Null-check before reading .value.

Example 3 — vs getAttributeNS()

Same lookup — string API vs Attr node API.

JavaScript
const el = document.getElementById("box");
const out = document.getElementById("out");
const XLINK = "http://www.w3.org/1999/xlink";

el.setAttributeNS(XLINK, "xlink:href", "https://example.com");
const asString = el.getAttributeNS(XLINK, "href");
const asNode = el.getAttributeNodeNS(XLINK, "href");

out.textContent =
  "string: " + asString + "\n" +
  "node.value: " + asNode.value + "\n" +
  "namespaceURI: " + asNode.namespaceURI;
Try It Yourself

How It Works

  1. Same inputs. Both APIs take namespace + local name.
  2. String path. getAttributeNS returns the value directly.
  3. Node path. getAttributeNodeNS returns Attr for extra properties.
  4. Pick by need. Values → NS string API; node details → this method.

Browser Support

Element.getAttributeNodeNS() is Baseline Widely available on MDN (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.

✓ Baseline Widely available

Element.getAttributeNodeNS()

Namespaced Attr lookup by URI + local name. Core DOM — available everywhere modern.

2015+ Widely available
Google Chrome All modern
Supported
Mozilla Firefox All modern
Supported
Apple Safari All modern
Supported
Microsoft Edge All modern
Supported
Opera All modern
Supported
Internet Explorer Supported
Supported
getAttributeNodeNS() Baseline

Bottom line: Safe for modern browsers and IE. Use for SVG/XML namespaces; prefer getAttributeNode for plain HTML.

Frequently Asked Questions

It returns the Attr node for a namespaced attribute on an element (MDN). You pass a namespace URI (or null) and the attribute’s local name.
No. MDN marks Element.getAttributeNodeNS() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
Two values: namespace (namespace URI string, or null for no explicit namespace) and localName (the attribute’s local name, for example "href").
getAttributeNS() returns the attribute value as a string (or null). getAttributeNodeNS() returns the Attr object so you can read name, value, namespaceURI, and ownerElement.
MDN: for HTML documents when you do not need a specific namespace, use getAttributeNode(). Use getAttributeNodeNS() for namespaced attributes (common in SVG and XML).
The method returns null when no attribute matches that namespace and local name.

Next: getAttributeNS()

Read a namespaced attribute value as a string.

getAttributeNS() →

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.

15 people found this page helpful