0Pricing
JavaScript Academy · Lesson

Creating and Inserting Elements

Build new nodes with createElement and append.

Building New Elements

Instead of writing HTML strings, you can construct elements in code. This avoids XSS and gives you a real element reference to configure.

createElement

document.createElement makes a new, detached element. It exists in memory but is not yet in the page.

const li = document.createElement("li");
li.textContent = "New item";

All lessons in this course

  1. Changing Text and HTML Content
  2. Creating and Inserting Elements
  3. Modifying Attributes and Classes
  4. Removing and Replacing Nodes
← Back to JavaScript Academy