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.
To create an element without specifying a namespace URI, use the createElement() method.
Syntax
createElementNS(namespaceURI, qualifiedName)
createElementNS(namespaceURI, qualifiedName, options)
Parameters
namespaceURI-
A string that specifies the
namespaceURIto associate with the element. Some important namespace URIs are: qualifiedName-
A string containing the qualified name of the new element. The
nodeNameproperty of the created element is initialized with this value.The format of the qualified name is
prefix:localNameorlocalName, where the parts are defined as:prefixOptional-
A "short alias" for the namespace. The prefix is optional, but if it is specified the
namespaceURIparameter must also be specified. If the prefix is set toxmlorxmlns, thenamespaceURImust be set tohttp://www.w3.org/XML/1998/namespaceorhttp://www.w3.org/2000/xmlns/, respectively.The value is used to initialize the new element's
prefixproperty. If not set, its value isnull. localName:-
The local name of the element. The value is used to initialize the new Element's
localNameproperty.
optionsOptional-
An object with the following optional properties (note that only one of
isandcustomElementRegistrymay be set):isOptional-
A string defining the tag name for a custom element (that was previously defined using
customElements.define()). The new element will be given anisattribute whose value is the custom element's tag name. See Web component example for more details. customElementRegistryOptional-
A
CustomElementRegistrythat sets the Scoped custom element registry of a custom element.
For backwards 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
NamespaceErrorDOMException-
Thrown if the
namespaceURIvalue is:- not a valid namespace URI
- set to the empty string when
prefixhas a value - not the value
http://www.w3.org/XML/1998/namespaceorhttp://www.w3.org/2000/xmlns/whenprefixis set toxmlorxmlns, respectively.
InvalidCharacterErrorDOMException-
Thrown if either the
prefixorlocalNameis not valid:- The
prefixmust have at least one character, and cannot contain ASCII whitespace,NULL,/, or>(U+0000, U+002F, or U+003E, respectively). - The
localNameis 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),
- it starts with an alphabet character and does not contain ASCII whitespace,
Note: Earlier versions of the specification were more restrictive, requiring that the
qualifiedNamebe a valid XML name. - The
NotSupportedErrorDOMException-
Thrown if both the
isandcustomElementRegistryoptions are specified.
Examples
>Basic usage
This creates a new <div> element in the XHTML namespace and
appends it to the vbox element. Although this is not an extremely useful XUL document, it does demonstrate the use of
elements from two different namespaces within a single document:
<?xml version="1.0"?>
<page xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
xmlns:html="http://www.w3.org/1999/xhtml"
title="||Working with elements||"
onload="init()">
<script><![CDATA[
let container;
let newDiv;
let textNode;
function init() {
container = document.getElementById("ContainerBox");
newDiv = document.createElementNS("http://www.w3.org/1999/xhtml", "div");
textNode = document.createTextNode(
"This is text that was constructed dynamically with createElementNS and createTextNode then inserted into the document using appendChild.",
);
newDiv.appendChild(textNode);
container.appendChild(newDiv);
}
]]></script>
<vbox id="ContainerBox" flex="1">
<html:div>
The script on this page will add dynamic content below:
</html:div>
</vbox>
</page>
Note: The example given above uses inline script which is not recommended in XHTML documents. This particular example is actually an XUL document with embedded XHTML, however, the recommendation still applies.
Specifications
| Specification |
|---|
| DOM> # ref-for-dom-document-createelementns①> |