document.createElement() creates a new HTML element in memory. The element does not appear on the page until it is inserted into the DOM with a method such as append(), appendChild(), prepend(), or insertBefore().
const button = document.createElement("button");
button.textContent = "New Button";
document.body.append(button);
Syntax:
const element = document.createElement("tagName");
Demo: Create an element dynamically
The simplest workflow has three steps: create the element, set its content or properties, and insert it into the DOM.
function addElement() {
const newButton = document.createElement("button");
newButton.textContent = "New Button";
document.body.appendChild(newButton);
}
The original plus2net example used appendChild(); it remains valid. Modern code can also use append(), which can append nodes and text.
You do not have to append to document.body. Find any existing parent and add the new element inside it.
const message = document.createElement("p");
message.textContent = "JavaScript section";
const parent = document.getElementById("my_div");
parent.append(message);
If the parent might not exist, check it before trying to append:
const parent = document.getElementById("my_div");
if (parent) {
const message = document.createElement("p");
message.textContent = "Added safely";
parent.append(message);
}
The original example inserts a new <li> into an ordered list. insertBefore(newNode, referenceNode) places a node before an existing child. If the reference node is null, it appends at the end.
const newItem = document.createElement("li");
newItem.textContent = "Dinner";
const list = document.getElementById("my_list");
list.insertBefore(newItem, list.children[2] || null);
After creating an element, set normal DOM properties when a direct property exists. Use setAttribute() when you need a specific HTML attribute.
const link = document.createElement("a");
link.href = "https://www.plus2net.com/";
link.textContent = "Visit plus2net";
link.classList.add("btn", "btn-primary");
link.setAttribute("aria-label", "Visit plus2net home page");
For custom data-* attributes, the dataset property is convenient:
const row = document.createElement("div");
row.dataset.userId = "42";
Use textContent for normal text. The older document.createTextNode() method is still valid and is useful when you explicitly need a text node.
const paragraph = document.createElement("p");
const text = document.createTextNode("Plain text");
paragraph.appendChild(text);
innerHTML parses a string as HTML. Do not insert untrusted user input with innerHTML, because unsafe markup can create cross-site scripting problems. For plain text, textContent is the safer default.
| Method | Use |
|---|---|
parent.appendChild(node) | Append one node and return the appended node. |
parent.append(node, text) | Append one or more nodes or text strings. |
parent.prepend(node) | Insert at the beginning of a parent. |
parent.insertBefore(node, reference) | Insert before a chosen child node. |
element.before(node) / after(node) | Insert relative to an existing element. |
createElement() alone does not display the element.const or let.innerHTML for text: prefer textContent unless HTML parsing is intended.null.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.