Document: createElementNS() 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.

* Some parts of this feature may have varying levels of support.

The createElementNS() method of the Document interface creates a new element with the specified namespace URI and qualified name.

This is useful in mixed-namespace documents, such as SVG or MathML embedded in HTML, where the parser cannot reliably infer the namespace.

The createElement() method is simpler if you want to create a plain HTML element.

Syntax

js
createElementNS(namespaceURI, qualifiedName)
createElementNS(namespaceURI, qualifiedName, options)

Parameters

namespaceURI

A string that specifies the namespaceURI to associate with the element. 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 element. The nodeName property of the created element 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 element's prefix property. Defaults to null.

localName

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

options Optional

An object with the following optional properties (note that only one of is and customElementRegistry may be set):

is Optional

A string defining the tag name for a custom element previously defined using customElements.define(). The new element will be given an is attribute whose value is the custom element's tag name.

customElementRegistry Optional

A CustomElementRegistry that sets the Scoped custom element registry of a custom element.

For backward compatibility, some browsers allow you to pass a string here instead of an object, where the string's value is the custom element's tag name. See Extending native HTML elements for more information on how to use this parameter.

Return value

The new Element.

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 is a valid element name if it has a length of at least 1 and:
    • it starts with an alphabet character and does not contain ASCII whitespace, NULL, /, or > (U+0000, U+002F, or U+003E, respectively).
    • it starts with : (U+003A), _ (U+005F), or any characters in the range U+0080 to U+10FFFF (inclusive), and the remaining code points only include those same characters along with the ASCII alphanumeric characters, - (U+002D), and . (U+002E),

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

NotSupportedError DOMException

Thrown if both the is and customElementRegistry options are specified.

Examples

Basic usage

This shows how to create a new <div> element in the XHTML namespace.

js
const divElementXHTML = document.createElementNS(
  "http://www.w3.org/1999/xhtml",
  "div",
);

// This is equivalent!
const divElementHTML = document.createElement("div");

Create an SVG element

This example shows how you might create an SVG element (SVGSVGElement) and append it to the HTML <body> element.

Using createElementNS() with the SVG namespace is necessary when working with an HTML document. If you were to call createElement("svg"), an HTMLUnknownElement would be returned, and the SVG would not be rendered.

js
const svgNS = "http://www.w3.org/2000/svg";

const svg = document.createElementNS(svgNS, "svg");
svg.setAttribute("width", "100");
svg.setAttribute("height", "100");

const circle = document.createElementNS(svgNS, "circle");
circle.setAttribute("cx", "50");
circle.setAttribute("cy", "50");
circle.setAttribute("r", "40");
circle.setAttribute("fill", "steelblue");

svg.appendChild(circle);
document.body.appendChild(svg);

Specifications

Specification
DOM
# ref-for-dom-document-createelementns①

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
createElementNS
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote
footnote Doesn't conform to the DOM spec for XUL and XHTML documents: localName and namespaceURI are not set to null on the created element.
Opera – Full support
Opera 12.1 (Release date: 2012-11-20)
footnote Full support
Safari – Full support
Safari 1 (Release date: 2003-06-23)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote Full support
Opera Android – Full support
Opera Android 12.1 (Release date: 2012-10-09)
footnote Full support
Safari on iOS – Full support
Safari on iOS 1 (Release date: 2007-06-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 1 (Release date: 2007-06-29)
footnote Full support
Emoji and more Unicode in element and attribute names and namespace prefix (valid names match HTML parser)
Chrome – Full support
Chrome 143 (Release date: 2025-12-02)
footnote Full support
Edge – Full support
Edge 143 (Release date: 2025-12-05)
footnote Full support
Firefox – Full support
Firefox 149 (Release date: 2026-03-24)
footnote Full support
Opera – Full support
Opera 127 (Release date: 2026-02-02)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 143 (Release date: 2025-12-02)
footnote Full support
Firefox for Android – Full support
Firefox for Android 149 (Release date: 2026-03-24)
footnote Full support
Opera Android – Full support
Opera Android 94 (Release date: 2026-01-13)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Full support
WebView Android 143 (Release date: 2025-12-02)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support
options parameter
Chrome – Full support
Chrome 56 (Release date: 2017-01-25)
footnote
footnote For backwards compatibility, the options parameter can be an object or a string with the custom element tag name, although the string version is deprecated.
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote
footnote For backwards compatibility, the options parameter can be an object or a string with the custom element tag name, although the string version is deprecated.
Firefox – Full support
Firefox 50 (Release date: 2016-11-15)
footnote
footnote Firefox accepts a string instead of an object here, but only from version 51 onwards. In version 50, options must be an object.
Opera – Full support
Opera 43 (Release date: 2017-02-07)
footnote
footnote For backwards compatibility, the options parameter can be an object or a string with the custom element tag name, although the string version is deprecated.
Safari – Full support
Safari 26 (Release date: 2025-09-15)
footnote Full support
Chrome Android – Full support
Chrome Android 56 (Release date: 2017-02-01)
footnote
footnote For backwards compatibility, the options parameter can be an object or a string with the custom element tag name, although the string version is deprecated.
Firefox for Android – Full support
Firefox for Android 50 (Release date: 2016-11-15)
footnote
footnote Firefox for Android accepts a string instead of an object here, but only from version 51 onwards. In version 50, options must be an object.
Opera Android – Full support
Opera Android 43 (Release date: 2017-09-27)
footnote
footnote For backwards compatibility, the options parameter can be an object or a string with the custom element tag name, although the string version is deprecated.
Safari on iOS – Full support
Safari on iOS 26 (Release date: 2025-09-15)
footnote Full support
Samsung Internet – Full support
Samsung Internet 6 (Release date: 2017-08-23)
footnote
footnote For backwards compatibility, the options parameter can be an object or a string with the custom element tag name, although the string version is deprecated.
WebView Android – Full support
WebView Android 56 (Release date: 2017-02-01)
footnote
footnote For backwards compatibility, the options parameter can be an object or a string with the custom element tag name, although the string version is deprecated.
WebView on iOS – Full support
WebView on iOS 26 (Release date: 2025-09-15)
footnote Full support
customElementRegistry option
Chrome – Full support
Chrome 146 (Release date: 2026-03-10)
footnote Full support
Edge – Full support
Edge 146 (Release date: 2026-03-13)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 130 (Release date: 2026-04-08)
footnote Full support
Safari – Full support
Safari 26 (Release date: 2025-09-15)
footnote Full support
Chrome Android – Full support
Chrome Android 146 (Release date: 2026-03-10)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 97 (Release date: 2026-04-16)
footnote Full support
Safari on iOS – Full support
Safari on iOS 26 (Release date: 2025-09-15)
footnote Full support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Full support
WebView Android 146 (Release date: 2026-03-10)
footnote Full support
WebView on iOS – Full support
WebView on iOS 26 (Release date: 2025-09-15)
footnote Full support
is option
Chrome – Full support
Chrome 56 (Release date: 2017-01-25)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 43 (Release date: 2017-02-07)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 56 (Release date: 2017-02-01)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 43 (Release date: 2017-09-27)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 6 (Release date: 2017-08-23)
footnote Full support
WebView Android – Full support
WebView Android 56 (Release date: 2017-02-01)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.

See also