Creating Elements

Using createElement to build dynamic content

Lesson 56
+25 XP
Step 1 of 5

Creating New Elements

The createElement() method allows you to create new HTML elements dynamically using JavaScript!

// Create a new paragraph element
let newParagraph = document.createElement("p");

// Set its text content
newParagraph.textContent = "This is a new paragraph!";

// Add a class
newParagraph.classList.add("my-paragraph");

console.log(newParagraph); // Element created but not yet in the page!

Important: Creating an element doesn't automatically add it to the page. You need to append it to an existing element!