×

javascript · level 2 · unit 3 · lesson 1

  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

Make an element with , then it.

document.createElement("li") makes a fresh <li> element. At first it floats off the page, like a jigsaw piece still in your hand: it exists, but nobody can see it.

Fill it in by setting its .textContent or adding classes. Then append it: guests.append(guest) puts it inside the list, after everything already there. Only then does it appear.

Each call to createElement makes a new, separate element, so make one for every thing you want to show.

<h2>Party guests</h2>
<ul id="guests">
  <li>Aisha</li>
</ul>
const guests = document.querySelector("#guests");
const guest = document.createElement("li");
guest.textContent = "Ben";
guests.append(guest);

Press run it to bring the page to life.

console

nothing logged yet

The tag name goes in without angle brackets: createElement("li"), not "<li>".