Forem

Cover image for Understanding createElement() in JavaScript
DHANRAJ S
DHANRAJ S

Posted on

Understanding createElement() in JavaScript

1. What is createElement()?

createElement() is a built-in JavaScript method that lets you create a brand new HTML element from scratch — without touching your HTML file.

You can create a button, a paragraph, a div, or any HTML tag you want — right from JavaScript.

Basic Syntax:

const newElement = document.createElement('tagName');
Enter fullscreen mode Exit fullscreen mode

For example, to create a new paragraph:

const para = document.createElement('p');
para.textContent = "Hello, I am a new paragraph!";
Enter fullscreen mode Exit fullscreen mode

This creates a <p> element in memory. But it won't appear on the page yet — you have to place it somewhere.


2. Why Do We Use createElement()?

Imagine you run an online to-do list app. Every time the user types a task and clicks "Add", a new item should appear. You cannot hardcode that in HTML — you don't know what the user will type!

That's where createElement() saves the day. It lets you dynamically add content to the page based on user actions, API data, or any logic.


3. Add Element at the LAST Position

This is the most common use case. Use appendChild() to add your new element at the end of a parent.

Think of it like adding a new dish at the bottom of the menu.

// script.js

const list = document.getElementById('myList');

const newItem = document.createElement('li');
newItem.textContent = "Item 4 (added at last)";

list.appendChild(newItem);
Enter fullscreen mode Exit fullscreen mode

Result:

appendChild() always adds the element as the last child of the parent.


4. Add Element at the FIRST Position

Now say you want the new item to appear at the top. Use insertBefore() with the first child as the reference.

Like putting a "Chef's Special" dish at the very top of the menu.

const list = document.getElementById('myList');

const newItem = document.createElement('li');
newItem.textContent = "Item 0 (added at first)";

// Insert before the first existing child
list.insertBefore(newItem, list.firstChild);
Enter fullscreen mode Exit fullscreen mode

Result:

list.firstChild points to the first item in the list. By passing it to insertBefore(), we place our new element before it.

💡 Tip: Use list.firstElementChild instead of list.firstChild to avoid accidentally targeting text nodes (whitespace) in some browsers.

list.insertBefore(newItem, list.firstElementChild);
Enter fullscreen mode Exit fullscreen mode

5. Add Element at a SPECIFIC Position

What if you want the new item to appear after Item 2? You pick the exact reference point.

Like inserting a new dish between "Starters" and "Mains" on the menu.

const list = document.getElementById('myList');

const newItem = document.createElement('li');
newItem.textContent = "Item 2.5 (added in between)";

// Get all current items
const items = list.getElementsByTagName('li');

// Insert after Item 2, which is index 1 (0-based)
// To insert AFTER index 1, we reference index 2
list.insertBefore(newItem, items[2]);
Enter fullscreen mode Exit fullscreen mode

Result:
insertBefore(newElement, referenceElement) places the new element right before the reference. So if you pass items[2] (which is Item 3), the new item lands just before it — which is after Item 2.


6. Bonus: Using insertAdjacentElement() for More Control

There's another method that gives you 4 precise positions in one neat package:

targetElement.insertAdjacentElement(position, newElement);
Enter fullscreen mode Exit fullscreen mode
Position Where it goes
'beforebegin' Before the target element itself
'afterbegin' Inside target, before its first child
'beforeend' Inside target, after its last child
'afterend' After the target element itself

Example:

const list = document.getElementById('myList');
const items = list.getElementsByTagName('li');

const newItem = document.createElement('li');
newItem.textContent = "Inserted using insertAdjacentElement";

// Insert after Item 2 (index 1)
items[1].insertAdjacentElement('afterend', newItem);
Enter fullscreen mode Exit fullscreen mode

This places the new item right after Item 2. Clean and readable!


7. Quick Summary of All Methods

Goal Method
Add at the end parent.appendChild(newEl)
Add at the beginning parent.insertBefore(newEl, parent.firstElementChild)
Add at a specific spot parent.insertBefore(newEl, referenceEl)
Fine-grained positioning target.insertAdjacentElement(position, newEl)

3 Golden Rules

  • ✅ Always create the element first with createElement(), then place it with appendChild() or insertBefore()
  • ✅ Use firstElementChild instead of firstChild to avoid whitespace issues
  • insertBefore(newEl, ref) places the new element before the reference — so to insert after item N, pass item N+1 as the reference

JavaScript DOM createElement visual guide

Top comments (0)