Document: createElement() 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 createElement() method of the Document interface creates a new HTMLElement that has the specified localName.

If localName isn't recognized, the method creates an HTMLUnknownElement.

Syntax

js
createElement(localName)
createElement(localName, options)

Parameters

localName

A string that specifies the type of element to be created. Don't use qualified names (like "html:a") with this method. When called on an HTML document, createElement() converts localName to lowercase before creating the element. In Firefox, Opera, and Chrome, createElement(null) works like createElement("null").

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. See Web component example for more details.

customElementRegistry Optional

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

Return value

The new Element.

Note: A new HTMLElement is returned if the document is an HTMLDocument, which is the most common case. Otherwise a new Element is returned.

Exceptions

InvalidCharacterError