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.

The createAttributeNS() method of the Document interface creates a new attribute node with the specified namespace URI and qualified name.

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 namespaceURI to associate with the attribute, or the empty string. Some important namespace URIs are:

HTML

http://www.w3.org/1999/xhtml

SVG

http://www.w3.org/2000/svg

MathML

http://www.w3.org/1998/Math/MathML

qualifiedName

A string containing the qualified name of the new attribute. The name property of the created attribute is initialized with this value.

The format of the qualified name is prefix:localName or localName, where the parts are defined as:

prefix Optional

A "short alias" for the namespace. The prefix is optional, but if it is specified the namespaceURI parameter must also be specified. If the prefix is set to xml or xmlns, the namespaceURI must be set to http://www.w3.org/XML/1998/namespace or http://www.w3.org/2000/xmlns/, respectively.

The value is used to initialize the new attribute's prefix property. If not set, its value is null.

localName:

The local name of the attribute. The value is used to initialize the new attribute's localName property.

Return value

The new Attr node.

Exceptions

NamespaceError DOMException

Thrown if the namespaceURI value is:

  • not a valid namespace URI
  • set to the empty string when prefix has a value
  • not the value http://www.w3.org/XML/1998/namespace or http://www.w3.org/2000/xmlns/ when prefix is set to xml or xmlns, respectively.
InvalidCharacterError DOMException

Thrown if either the prefix or localName is not valid:

  • The prefix must have at least one character, and cannot contain ASCII whitespace, NULL, / , or > (U+0000, U+002F, or U+003E, respectively).
  • The localName must have at least 1 character, and may not contain ASCII whitespace, NULL, / , = or > (U+0000, U+002F, U+003D or U+003E, respectively).

Note: Earlier versions of the specification were more restrictive, requiring that the localName be a valid XML name.

Examples

Basic usage

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
# dom-document-createattributens

Browser compatibility

See also