JavaScript Element getAttributeNodeNS() Method

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

What You’ll Learn

Element.getAttributeNodeNS() is an instance method that returns the Attr node for a namespaced attribute. Learn namespace URI + qualified name, how it compares to getAttributeNode() and getAttributeNS(), common SVG xlink:href usage, and five try-it labs aligned with MDN.

01

Kind

Instance method

02

Returns

Attr or null

03

Args

namespace, name

04

Scope

Namespaced attrs

05

vs getAttrNS

Attr vs string

06

Status

Baseline widely

Introduction

Plain HTML attributes like id or class usually work with getAttribute() or getAttributeNode(). SVG and XML documents also use namespaced attributes such as xlink:href, which belong to a namespace URI.

getAttributeNodeNS(namespace, nodeName) looks up that namespaced attribute and returns the full Attr object—not just the string value.

💡
Beginner tip

If you only need the value, getAttributeNS(namespace, name) is simpler. Use getAttributeNodeNS() when you need Attr properties like namespaceURI or ownerElement (MDN).

This page is part of JavaScript Element. Pair it with the non-namespaced getAttributeNode() tutorial.

Understanding the getAttributeNodeNS() Method

Calling el.getAttributeNodeNS(namespace, nodeName) finds an attribute that matches both the namespace URI and the qualified name, then returns its Attr node.

  • It is an instance method on Element.
  • namespace — the attribute’s namespace URI (e.g. XLink namespace).
  • nodeName — the qualified name (e.g. "xlink:href").
  • Returns an Attr node, or null if not found.
  • More specific than getAttributeNode() for namespaced attributes (MDN).
  • Setter counterpart: setAttributeNodeNS() (MDN).

📝 Syntax

General form of Element.getAttributeNodeNS (MDN):

JavaScript
getAttributeNodeNS(namespace, nodeName)

Parameters

  • namespace — a string specifying the namespace URI of the attribute.
  • nodeName — a string specifying the qualified name of the attribute.

Return value

The Attr node for the specified namespaced attribute, or null if it does not exist.

Common patterns

JavaScript
const XLINK = "http://www.w3.org/1999/xlink";
const link = document.querySelector("svg a");

const hrefAttr = link.getAttributeNodeNS(XLINK, "xlink:href");
if (hrefAttr) {
  console.log(hrefAttr.value);
  console.log(hrefAttr.namespaceURI);
}

// Value only? Use getAttributeNS:
console.log(link.getAttributeNS(XLINK, "xlink:href"));

⚡ Quick Reference

GoalCode
Namespaced Attr nodeel.getAttributeNodeNS(ns, "xlink:href")
Read value from Attrattr.value
Namespaced string valueel.getAttributeNS(ns, "xlink:href")
Non-namespaced HTML attrel.getAttributeNode("id")
Not foundnull
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Element.getAttributeNodeNS().

Returns
Attr|null

Namespaced node

Baseline
widely

Since July 2015

Two args
ns + name

URI + qualified

Simple read
getAttrNS

Usually enough

📋 Namespaced attribute APIs

getAttributeNodeNS()getAttributeNS()getAttributeNode()
NamespaceYes (URI required)Yes (URI required)No (HTML attrs)
ReturnsAttr or nullstring or nullAttr or null
Typical docsSVG, XMLSVG, XMLHTML
Attr detailsYesNoYes (non-NS)
Best forNamespaced Attr inspectionNamespaced value readsPlain attribute Attr

Examples Gallery

Examples follow MDN Element.getAttributeNodeNS() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Read a namespaced xlink:href Attr on SVG.

Example 2 — Wrong Namespace Returns null

Using the wrong URI or name finds nothing.

JavaScript
const anchor = document.querySelector("svg a");

console.log(
  anchor.getAttributeNodeNS("http://wrong.namespace", "xlink:href")
);
// null
Try It Yourself

How It Works

Namespace URIs must be exact. A typo or wrong prefix mapping returns null.

📈 Practical Patterns

namespaceURI, comparison with getAttributeNS, and set + get.

Example 3 — Read namespaceURI on the Attr

Why you might want the Attr object instead of a plain string.

JavaScript
const XLINK = "http://www.w3.org/1999/xlink";
const anchor = document.querySelector("svg a");
const attr = anchor.getAttributeNodeNS(XLINK, "xlink:href");

console.log(attr.namespaceURI);
// "http://www.w3.org/1999/xlink"

console.log(attr.name);
// "xlink:href"
Try It Yourself

How It Works

MDN: use getAttributeNodeNS() when you need Attr instance properties, not only the value string.

Example 4 — vs getAttributeNS()

Same attribute, different return types.

JavaScript
const XLINK = "http://www.w3.org/1999/xlink";
const anchor = document.querySelector("svg a");

const attr = anchor.getAttributeNodeNS(XLINK, "xlink:href");
const str = anchor.getAttributeNS(XLINK, "xlink:href");

console.log(typeof attr);  // "object"
console.log(typeof str);   // "string"
console.log(attr.value === str);
// true
Try It Yourself

How It Works

MDN recommends getAttributeNS() for values only. getAttributeNodeNS() when you need the Attr node.

Example 5 — After setAttributeNS()

Set a namespaced attribute, then read it back with getAttributeNodeNS().

JavaScript
const XLINK = "http://www.w3.org/1999/xlink";
const el = document.createElementNS("http://www.w3.org/2000/svg", "a");

el.setAttributeNS(XLINK, "xlink:href", "https://example.com");
el.setAttributeNS(XLINK, "xlink:show", "new");

const showAttr = el.getAttributeNodeNS(XLINK, "xlink:show");

console.log(showAttr.value);
// "new"
Try It Yourself

How It Works

Namespaced attributes are typically created with setAttributeNS(). getAttributeNodeNS() retrieves the resulting Attr node.

🚀 Common Use Cases

  • Inspecting SVG xlink:href and similar namespaced links.
  • XML tooling that needs Attr.namespaceURI metadata.
  • Library code that moves Attr nodes with setAttributeNodeNS().
  • Teaching the difference between HTML and XML/SVG attribute models.
  • Verifying namespace URIs after parsing XML documents.
  • Legacy DOM code that works with Attr objects rather than strings.

🧠 How getAttributeNodeNS() Resolves Namespaced Attributes

1

Pass namespace URI + name

getAttributeNodeNS(uri, "xlink:href")

Args
2

Match namespaced attribute

Browser finds the Attr whose namespace and qualified name match.

Lookup
3

Return Attr or null

Attr node with value, name, namespaceURI—or null.

Result
4

Use or fall back

For values only, getAttributeNS() is simpler; for plain HTML attrs use getAttributeNode().

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available, July 2015).
  • For non-namespaced HTML attributes, use getAttributeNode() (MDN).
  • Namespace URI must be exact—not the prefix alone.
  • Setter counterpart: setAttributeNodeNS().
  • Value-only reads: getAttributeNS().
  • Related: getAttributeNode(), getAttribute(), JavaScript hub.

Browser Support

Element.getAttributeNodeNS() is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.getAttributeNodeNS()

Get namespaced Attr nodes for SVG and XML — use getAttributeNS() for simple value reads.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop & Mobile
Yes
Apple Safari Supported · macOS & iOS
Yes
Opera Supported · Modern versions
Yes
Internet Explorer Supported in legacy IE
Yes
getAttributeNodeNS() Excellent

Bottom line: Use getAttributeNodeNS() for namespaced Attr inspection in SVG/XML. Pass the correct namespace URI and qualified name; prefer getAttributeNS() when you only need the string value.

Conclusion

Element.getAttributeNodeNS() is the namespaced version of getAttributeNode(). Pass a namespace URI and qualified name to get an Attr node from SVG or XML documents.

Continue with getAttributeNode(), getAttribute(), shadowRoot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use the correct namespace URI (not just the prefix)
  • Null-check before reading attr.value
  • Prefer getAttributeNS() for value-only reads
  • Use getAttributeNode() for plain HTML attrs
  • Pair with setAttributeNS() when creating attrs

❌ Don’t

  • Pass "xlink" instead of the full namespace URI
  • Use for ordinary id / class on HTML
  • Assume return type is a string
  • Confuse qualified name with local name only
  • Forget SVG needs createElementNS for elements too

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.getAttributeNodeNS()

Namespaced Attr nodes for SVG and XML.

5
Core concepts
📄 02

Args

URI + name

Two
✍️ 03

SVG/XML

namespaced

Scope
04

Baseline

widely available

Status
05

Values

getAttributeNS

Simpler

❓ Frequently Asked Questions

It returns the Attr node for a namespaced attribute on an element. You pass a namespace URI and the qualified attribute name (for example xlink:href).
No. MDN marks Element.getAttributeNodeNS() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
Two strings: namespace (the attribute namespace URI) and nodeName (the qualified name, often prefix:localName like xlink: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.
For non-namespaced attributes in HTML documents, use getAttributeNode(). Use getAttributeNodeNS() when the attribute belongs to a specific namespace (common in SVG and XML).
The method returns null, same as other attribute lookup methods when nothing matches.
Did you know?

MDN notes that getAttributeNodeNS() is more specific than getAttributeNode() because it targets attributes in a particular namespace. For everyday HTML id and class attributes, the non-NS method is the right tool.

More Element Topics

Browse Element methods and properties to keep building your DOM skills.

getAttributeNode() →

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