Skip to content

Try our new Crash Courses!

Buy one of our new Crash Courses, now hosted on Teachable.

Create element – JavaScript


To create and append element using JavaScript, use the createElement() and appendChild() methods:

// Create new element
const newParagraph = document.createElement('p');

// Add text content to new element
newParagraph.textContent = 'Test paragraph';

// Select parent element
const parentDiv = document.querySelector('div');

// Append new element to parent element


You can also insert the newly created element before another element. Given the following HTML code:

  <p>First paragraph</p>

You can use the following JavaScript code to insert a new paragraph before the paragraph that’s already inside the div:

const myDiv = document.querySelector('div');
const firstParagraph = document.querySelector('p');

myDiv.insertBefore(newParagraph, firstParagraph);



Recreate the JS code from the Demo section in your JS file. Then try customizing the text inside the element.


Manipulating documents on MDN (see the section called “Creating and placing new nodes”)

createElement() on MDN

appendChild() on MDN

insertBefore() on MDN

Back to: JavaScript Reference > JS DOM