← CodePrimer
JavaScript Beginner Dom

createElement and appendChild

How do I add a new element with JS?

Create + append pattern.

Sample
const p = document.createElement('p');
p.textContent = 'This is a new paragraph!';
document.body.appendChild(p);

Notes

Build nodes, set text, then attach to the tree.

Keywords: createElement · appendChild · dom

Related

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share