Document: createAttributeNS() method
{{ ApiRef("DOM") }}
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
{{domxref("Attr")}}
interface. The DOM does not enforce what sort of attributes can be
added to a particular element in this manner.
Syntax
createAttributeNS(namespaceURI, qualifiedName)
Parameters
namespaceURI
qualifiedName
- : A string that specifies the name of attribute to be created.
The
{{DOMxRef("Attr.name", "name")}}
property of the created attribute is initialized with the value ofqualifiedName
.
- : A string that specifies the name of attribute to be created.
The
Return value
The new {{domxref("Attr")}}
node.
Exceptions
NamespaceError
{{domxref("DOMException")}}
- : Thrown if the
namespaceURI
value is not a valid namespace URI.
- : Thrown if the
InvalidCharacterError
{{domxref("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.
- : Thrown if the
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
{{Specifications}}
Browser compatibility
{{Compat}}
See also
{{domxref("Document.createAttribute()")}}
{{domxref("Document.createElementNS()")}}
{{domxref("Element.setAttributeNS()")}}
{{domxref("Element.setAttributeNode()")}}
{{domxref("Element.setAttributeNodeNS()")}}