×

javascript · level 2 · unit 3 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

takes an element away, clears all.

Every element has a remove() method. Call it, and the element leaves the page along with everything inside it.

A neat trick: make a Done button along with each new item. Its listener is written inside the loop, so it remembers its own item and removes exactly that one, however many items there are.

To empty a parent completely, call replaceChildren() with nothing in the brackets. Every child goes in one step, which is handy before drawing a list again.

<ul id="jobs"></ul>
const jobs = document.querySelector("#jobs");
for (const job of ["Wash up", "Hoover", "Empty the bins"]) {
  const item = document.createElement("li");
  const done = document.createElement("button");
  item.textContent = job;
  done.textContent = "Done";
  done.addEventListener("click", () => { item.remove(); });
  item.append(done);
  jobs.append(item);
}

Press run it to bring the page to life.

console

nothing logged yet

Click a Done button and only its own job disappears.