Document: createAttributeNS() method
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheDocument.createAttributeNS() method creates a new attribute nodewith the specified namespace URI and qualified name, and returns it.The object created is a node implementing theAttr interface. The DOM does not enforce what sort of attributes can beadded to a particular element in this manner.
In this article
Syntax
createAttributeNS(namespaceURI, qualifiedName)Parameters
namespaceURIA string that specifies the
namespaceURIto associate with the attribute. Some important namespace URIs are:qualifiedNameA 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 newAttr node.
Exceptions
NamespaceErrorDOMExceptionThrown if the
namespaceURIvalue is not a valid namespace URI.InvalidCharacterErrorDOMExceptionThrown if the
qualifiedNamevalue is not a validXML name; for example, it starts with a number, hyphen, or period, or contains characters other than alphanumeric characters, underscores, hyphens, or periods.
Examples
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> # dom-document-createattributens> |