This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The Document.createAttributeNS() method creates a new attribute node with the specified namespace URI and qualified name, and returns it. The object created is a node implementing the Attr interface. The DOM does not enforce what sort of attributes can be added to a particular element in this manner.
createAttributeNS(namespaceURI, qualifiedName)
namespaceURIA string that specifies the namespaceURI to associate with the attribute. Some important namespace URIs are:
qualifiedNameA string that specifies the name of attribute to be created. The name property of the created attribute is initialized with the value of qualifiedName.
The new Attr node.
NamespaceError DOMException
Thrown if the namespaceURI value is not a valid namespace URI.
InvalidCharacterError DOMException
Thrown if the qualifiedName value is not a valid XML name; for example, it starts with a number, hyphen, or period, or contains characters other than alphanumeric characters, underscores, hyphens, or periods.
const node = document.getElementById("svg");
const a = document.createAttributeNS("http://www.w3.org/2000/svg", "viewBox");
a.value = "0 0 100 100";
node.setAttributeNode(a);
console.log(node.getAttribute("viewBox")); // "0 0 100 100"
| Specification |
|---|
| DOM> # dom-document-createattributens> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
createAttributeNS |
1 | 12 | 1 | ≤12.1 | 1 | 18 | 4 | ≤12.1 | 1 | 1.0 | 4.4 | 1 |
Document.createAttribute()Document.createElementNS()Element.setAttributeNS()Element.setAttributeNode()Element.setAttributeNodeNS()
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Document/createAttributeNS