JavaScript Element getAttributeNS() Method

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

What You’ll Learn

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

01

Kind

Instance method

02

Returns

string or null

03

Args

namespace, name

04

Scope

Namespaced attrs

05

vs getAttr

NS + URI needed

06

Status

Baseline widely

Introduction

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

getAttributeNS(namespace, name) looks up that namespaced attribute and returns its value as a string—or null if it does not exist.

💡
Beginner tip

MDN recommends getAttribute() for ordinary HTML attributes when you do not need a namespace. Use getAttributeNS() for SVG/XML namespaced attrs. Need the full Attr object? Use getAttributeNodeNS() instead.

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

Understanding the getAttributeNS() Method

Calling el.getAttributeNS(namespace, name) finds an attribute that matches both the namespace URI and the qualified name, then returns its value as a string.

  • It is an instance method on Element.
  • namespace — the attribute’s namespace URI (e.g. XLink namespace).
  • name — the qualified name (e.g. "xlink:href").
  • Returns a string, or null if not found (DOM4; MDN).
  • More specific than getAttribute() for namespaced attributes (MDN).
  • Setter counterpart: setAttributeNS() (MDN).

📝 Syntax

General form of Element.getAttributeNS (MDN):

JavaScript
getAttributeNS(namespace, name)

Parameters

  • namespace — a string specifying the namespace in which to look for the attribute.
  • name — a string specifying the qualified name of the attribute to look for.

Return value

The string value of the specified attribute, or null if it does not exist. MDN notes that very old browsers sometimes returned an empty string instead of null—use hasAttributeNS() when that matters.

Common patterns

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

const href = link.getAttributeNS(XLINK, "xlink:href");
if (href) {
  console.log(href);
}

// Safer existence check (MDN):
if (link.hasAttributeNS(XLINK, "xlink:href")) {
  console.log(link.getAttributeNS(XLINK, "xlink:href"));
}

⚡ Quick Reference

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

🔍 At a Glance

Four facts to remember about Element.getAttributeNS().

Returns
string|null

Attribute value

Baseline
widely

Since July 2015

Two args
ns + name

URI + qualified

HTML attrs
getAttribute

Usually enough

📋 Namespaced attribute APIs

getAttributeNS()getAttribute()getAttributeNodeNS()
NamespaceYes (URI required)Yes (URI required)No (HTML attrs)
Returnsstring or nullstring or nullAttr or null
Typical docsSVG, XMLHTMLSVG, XML
Attr detailsNoNoYes
Best forNamespaced value readsPlain attribute valuesNamespaced Attr inspection

Examples Gallery

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

📚 Getting Started

Read a namespaced xlink:href value 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.getAttributeNS("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

Custom namespaces, HTML embedding, and comparison with getAttributeNodeNS.

Example 3 — Custom Namespace Attribute

MDN-style test:foo on an SVG circle with a custom namespace URI.

JavaScript
const TEST_NS = "http://www.example.com/2014/test";
const circle = document.getElementById("target");

console.log(circle.getAttributeNS(TEST_NS, "foo"));
// "Hello namespaced attribute!"
Try It Yourself

How It Works

MDN: getAttributeNS() lets you target attributes in a particular namespace, not just by prefixed name alone.

Example 4 — SVG in HTML: getAttribute() Fallback

When SVG is embedded in HTML, MDN notes you may read prefixed attrs with getAttribute("test:foo").

JavaScript
const TEST_NS = "http://www.example.com/2014/test";
const circle = document.getElementById("target");

const viaNS = circle.getAttributeNS(TEST_NS, "foo");
const viaName = circle.getAttribute("test:foo");

console.log(viaNS);
console.log(viaName);
// Both return the same value in HTML documents
Try It Yourself

How It Works

In standalone SVG/XML, namespace URIs matter. In HTML documents, prefixed attribute names often work with plain getAttribute() (MDN).

Example 5 — vs getAttributeNodeNS()

Same attribute—string from getAttributeNS() vs Attr from getAttributeNodeNS().

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

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

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

How It Works

Prefer getAttributeNS() when you only need the value. Use getAttributeNodeNS() when you need Attr metadata.

🚀 Common Use Cases

  • Reading SVG xlink:href and similar namespaced link URLs.
  • XML/SVG tooling that reads custom namespace attribute values.
  • Library code paired with setAttributeNS() for round-trip reads.
  • Teaching the difference between HTML and XML/SVG attribute models.
  • Verifying namespace URIs after parsing XML documents.
  • Simple value reads without needing the full Attr object.

🧠 How getAttributeNS() Resolves Namespaced Attributes

1

Pass namespace URI + name

getAttributeNS(uri, "xlink:href")

Args
2

Match namespaced attribute

Browser finds the attribute whose namespace and qualified name match.

Lookup
3

Return string or null

The attribute value as a string—or null when not found (DOM4).

Result
4

Use or fall back

For plain HTML attrs use getAttribute(); for Attr metadata use getAttributeNodeNS().

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available, July 2015).
  • For non-namespaced HTML attributes, use getAttribute() (MDN).
  • Namespace URI must be exact—not the prefix alone.
  • Setter counterpart: setAttributeNS().
  • Attr object reads: getAttributeNodeNS().
  • MDN: use hasAttributeNS() before reading if the attribute might be missing on older browsers.
  • Related: getAttribute(), getAttributeNodeNS(), JavaScript hub.

Browser Support

Element.getAttributeNS() 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.getAttributeNS()

Read namespaced attribute values as strings in SVG and XML — use getAttribute() for plain HTML attributes.

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
getAttributeNS() Excellent

Bottom line: Use getAttributeNS() for namespaced value reads in SVG/XML. Pass the correct namespace URI and qualified name; prefer getAttribute() for ordinary HTML attributes.

Conclusion

Element.getAttributeNS() is the namespaced version of getAttribute(). Pass a namespace URI and qualified name to read a string value from SVG or XML documents.

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

💡 Best Practices

✅ Do

  • Use the correct namespace URI (not just the prefix)
  • Null-check before using the returned string
  • Prefer getAttribute() for plain HTML attributes
  • Use hasAttributeNS() when existence is uncertain (MDN)
  • 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 an Attr object
  • Confuse qualified name with local name only
  • Forget SVG needs createElementNS for elements too

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.getAttributeNS()

Namespaced string reads for SVG and XML.

5
Core concepts
📄 02

Args

URI + name

Two
✍️ 03

SVG/XML

namespaced

Scope
04

Baseline

widely available

Status
05

Values

direct string

Simple

❓ Frequently Asked Questions

It returns the string value of a namespaced attribute on an element. You pass a namespace URI and the qualified attribute name (for example xlink:href). If the attribute does not exist, it returns null.
No. MDN marks Element.getAttributeNS() 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 name (the qualified name, often prefix:localName like xlink:href).
getAttribute() reads non-namespaced HTML attributes by name only. getAttributeNS() also requires the namespace URI, which is needed for SVG and XML namespaced attributes.
getAttributeNS() returns the value as a string (or null). getAttributeNodeNS() returns the full Attr object when you need metadata like namespaceURI or ownerElement.
The method returns null when nothing matches. MDN recommends using hasAttributeNS() first if you need to distinguish missing attributes from empty values on older browsers.
Did you know?

MDN notes that getAttributeNS() differs from getAttribute() because it lets you specify the attribute as part of 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.

getAttribute() →

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