JavaScript Element getAttributeNS() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

In JavaScript, the getAttributeNS() method returns the string value of a namespaced attribute, or null if it is missing (MDN). Pass a namespace URI and the attribute’s local name.

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

Syntax

Call el.getAttributeNS. Start with this line.

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

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

Library declarations
JavaScript
el.getAttributeNS(namespace, localName)

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*Attribute value string, or null

JavaScript Examples

Example 1 — Read a Namespaced Value

MDN-style: set with setAttributeNS, read with getAttributeNS(ns, "foo").

JavaScript
const ns = "http://www.example.com/2014/test";
const el = document.createElement("div");

el.setAttributeNS(ns, "test:foo", "Hello namespaced attribute!");
console.log(el.getAttributeNS(ns, "foo"));
Try It Yourself

How It Works

  1. Pick a namespace URI. Any URI string that identifies the namespace.
  2. Set with a qualified name. setAttributeNS(ns, "test:foo", value) stores it under that namespace.
  3. Read with the local name. Second argument is "foo", not "test:foo" (MDN).
  4. Get a string. The attribute value comes back as text.

Example 2 — Missing Attribute → null

When the namespaced attribute is absent, the return value is null.

JavaScript
const ns = "http://www.example.com/2014/test";
const el = document.createElement("div");

console.log(el.getAttributeNS(ns, "foo")); // null
console.log(el.getAttributeNS(ns, "foo") === null);
Try It Yourself

How It Works

  1. No attribute set. The element has nothing in that namespace.
  2. Call getAttributeNS. Missing → null (MDN).
  3. Compare carefully. Use === null, not truthiness alone if empty string is valid.
  4. Optional guard. hasAttributeNS(ns, "foo") checks existence first.

Example 3 — HTML Document Caveat (MDN)

In HTML, prefixed attributes are often read with getAttribute("test:foo").

JavaScript
const ns = "http://www.example.com/2014/test";
const el = document.createElement("div");
el.setAttribute("test:foo", "Foo value");

console.log(el.getAttribute("test:foo"));     // "Foo value"
console.log(el.getAttributeNS(ns, "foo"));    // often null in HTML
Try It Yourself

How It Works

  1. HTML sets a prefixed name. setAttribute("test:foo", …) stores a literal name.
  2. Read with getAttribute. MDN’s HTML path uses the full "test:foo" string.
  3. getAttributeNS may miss it. HTML does not treat that as a real namespace (MDN).
  4. Use NS APIs in SVG/XML. Prefer setAttributeNS + getAttributeNS there.

Browser Support

Element.getAttributeNS() is Baseline Widely available (MDN: across browsers since July 2015). Safe to use in modern evergreen browsers.

✓ Baseline · Widely available

getAttributeNS()

Returns the string value of a namespaced attribute — or null if missing.

99% Widely available
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
getAttributeNS() Excellent

Bottom line: Use getAttributeNS for SVG/XML namespaces; use getAttribute for ordinary HTML attributes.

Frequently Asked Questions

It returns the string value of a namespaced attribute. You pass a namespace URI and the attribute’s local name. If that namespaced attribute is missing, it returns null (MDN).
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, or null for no explicit namespace) and localName (the local name only — for example "foo", not "test:foo").
For plain HTML attributes without a namespace, use getAttribute(). Use getAttributeNS() when the attribute belongs to a specific namespace (common in SVG/XML).
MDN: HTML documents do not support attribute namespaces the same way. In HTML markup, access a prefixed attribute with getAttribute("test:foo") instead of getAttributeNS.
getAttributeNS() returns the value as a string (or null). getAttributeNodeNS() returns the Attr node when you need metadata such as namespaceURI.

Next: getBoundingClientRect()

Learn how to read an element’s size and position in the viewport.

getBoundingClientRect tutorial →

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.

11 people found this page helpful