HTML DOM Document createElement()

Examples

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

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

Create a <p> element and append it to an element:

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

More examples below.


Description

The createElement() method creates an element node.



Syntax

document.createElement(type)

Parameters

Parameter Description
type Required.
The type of element to create.

Return Value

Type Description
NodeThe created element node.

More Examples

Create a button:

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

Browser Support

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

It is fully supported in all browsers:

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


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.