JavaScript AcademyProfessional Engineering

Learning object 38 of 100

Element creation laboratory

Create, insert, move and remove elements in a live preview and watch the page and the console change together.

Loading…

What it shows

createElement makes a new element that is not yet in the page. append, prepend, before and after insert it; inserting an element that is already in the page moves it rather than copying it. remove takes an element out, and replaceChildren swaps all of a parent's children in one step. Building elements and setting textContent is the safe way to show user data.

Guided experiment

  1. In Build a list, add a fourth item to the data array and run again.
  2. In Moving, predict which list holds Milk after the append.
  3. In Replace, call render twice. Why are there no duplicates?

Predict

An element already in list A is appended to list B. Where is it now?

Related

Lessons: Lesson 54: Creating and removing elements

Projects: Project 9: Shopping Cart Price Calculator

Source: web/assets/js/objects/object-038.js in the companion repository.