Document: createAttributeNS() method
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.
Syntax
js
createAttributeNS(namespaceURI, qualifiedName)
Parameters
namespaceURI-
A string that specifies the namespace URI to associate with the attribute. The
namespaceURIproperty of the created attribute is initialized with the value ofnamespaceURI. See Valid Namespace URIs. qualifiedName-
A string that specifies the name of attribute to be created. The
nameproperty of the created attribute is initialized with the value ofqualifiedName.
Return value
The new Attr node.
Exceptions
NamespaceErrorDOMException-
Thrown if the
namespaceURIvalue is not a valid namespace URI. InvalidCharacterErrorDOMException-
Thrown if the
qualifiedNamevalue 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.
Important Namespace URIs
Examples
js
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"
Specifications
| Specification |
|---|
| DOM Standard # dom-document-createattributens |
Browser compatibility
BCD tables only load in the browser