HTML DOM Document createElement() method

Definition and usage

createElement() The method creates an element node.

See also:

Element appendChild() method

Element insertBefore() method

Example

Example 1

Create a <p> element and attach it to the document:

const para = document.createElement("p");
para.innerText = "This is a paragraph";
document.body.appendChild(para);

Try it yourself

Example 2

Create a <p> element and attach it to the element:

const para = document.createElement("p");
para.innerHTML = "This is a paragraph.";
document.getElementById("myDIV").appendChild(para);

Try it yourself

Example 3

Create a button:

const btn = document.createElement("button");
btn.innerHTML = "Hello Button";
document.body.appendChild(btn);

Try it yourself

Syntax

document.createElement(tagName)

Parameter

Parameter Description
tagName

Required. The tag name of the element to be created.

HTML element names can be in any case.

XML element names need to be case-sensitive.

Return value

Type Description
Node The newly created element node has the specified tag name.

throw

If the tagName contains illegal characters, this method will throw an error code of INVALID_CHARACTER_ERR of the DOMException exception.

Browser support

document.createElement() It is a feature of DOM Level 1 (1998).

It is supported by all browsers:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support