document.createAttributeNS() is an instance method that creates a namespaced Attr node (see MDN Document: createAttributeNS()). Learn namespaceURI, qualifiedName, MDN’s xml:lang and SVG viewBox examples, how it compares to createAttribute() and setAttributeNS(), and five try-it labs.
01
Kind
Instance method
02
Args
URI + name
03
Returns
Attr node
04
Use for
namespaces
05
HTML null NS
"" URI
06
Status
Baseline
Fundamentals
Introduction
createAttribute() builds attribute nodes for the default (null) namespace. When an attribute name includes a prefix tied to a namespace — like xml:lang or xlink:href — you need the namespaced version: createAttributeNS(namespaceURI, qualifiedName).
MDN: the method returns an Attr node. Set attr.value, then attach it with setAttributeNode() or setAttributeNodeNS().
Document.createAttributeNS() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.createAttributeNS()
Creates namespaced Attr nodes — supported across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
createAttributeNS()Wide
Bottom line: Use createAttributeNS for namespaced Attr nodes; prefer setAttributeNS for simple name/value updates.
Wrap Up
Conclusion
document.createAttributeNS(namespaceURI, qualifiedName) creates a namespaced Attr node. Use it for attributes like xml:lang or when you need the node object before attaching. For most day-to-day updates, setAttributeNS() is simpler.
Use correct namespace URI for prefixed names (MDN)
Pass "" for null-namespace unprefixed attrs
Prefer setAttributeNS for simple updates
Set attr.value before attaching
Pair with setAttributeNodeNS when moving Attr nodes
❌ Don’t
Use createAttributeNS for plain HTML when setAttribute suffices
Mix wrong URI with xml / xmlns prefix (MDN)
Assume DOM validates attribute/element pairs (MDN)
Forget qualifiedName format prefix:localName
Confuse with createElementNS (creates elements)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about createAttributeNS()
Namespaced Attr nodes for XML, SVG, and XLink.
5
Core concepts
📝01
Args
URI + qName
MDN
🌐02
xml:lang
XML NS
example
📄03
Null NS
"" URI
viewBox
⚡04
Prefer
setAttributeNS
daily
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.createAttributeNS() creates a new attribute node with a specified namespace URI and qualified name. The result is an Attr object you can attach with setAttributeNode() or setAttributeNodeNS().
No. MDN marks Document.createAttributeNS() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
Use createAttributeNS() when the attribute belongs to a namespace — for example xml:lang (XML namespace) or xlink:href (XLink). For ordinary HTML attributes in the null namespace, createAttribute() or setAttribute() is enough.
MDN: in HTML documents most attributes are in the null namespace — pass the empty string "" for namespaceURI when creating unprefixed attributes like SVG viewBox.
MDN: a string like prefix:localName or just localName. For xml:lang, qualifiedName is "xml:lang" and namespaceURI is http://www.w3.org/XML/1998/namespace.
Yes. For most apps, element.setAttributeNS(namespace, name, value) is simpler than createAttributeNS + setAttributeNodeNS. Use createAttributeNS when you need an Attr node object.
Did you know?
MDN’s SVG viewBox example uses createAttributeNS("", "viewBox") — an empty namespace URI — and notes that in most cases you can skip the Attr node entirely and call svg.setAttribute("viewBox", "0 0 100 100") instead.