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)
Call
What 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 Value
Description
*
Attribute value string, or null
JavaScript Examples
Example 1 — Read a Namespaced Value
MDN-style: set with setAttributeNS, read with getAttributeNS(ns, "foo").
HTML sets a prefixed name.setAttribute("test:foo", …) stores a literal name.
Read with getAttribute. MDN’s HTML path uses the full "test:foo" string.
getAttributeNS may miss it. HTML does not treat that as a real namespace (MDN).
Use NS APIs in SVG/XML. Prefer setAttributeNS + getAttributeNS there.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll 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.