Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Add Elements to the Page

In this video, you built Priya’s players list from her data: a script that makes one list item per player and adds it to the page.

Key Points

  • document.createElement("li") makes a new list item. It exists only in JavaScript, held in a constant, until you add it to the page: the page doesn’t change.
  • Fill the new element with textContent, as you did for the date line. Watch out for innerHTML, which many tutorials use: it reads text as HTML, so a name a visitor typed could run as code. Use textContent instead.
  • An element’s append() method adds an element at the end of it. Check the result on the page and in the Elements panel.
  • In a loop, create a new element on every pass. With one createElement() above the loop, there’s only one element. An element can be in only one place, so append() moves it, never copies it: each pass changes its text and moves it to the end, where it already was, so the list ends with one item, Wei.
  • classList is an element’s list of classes. item.classList.add("new") gives a first-timer the class new, and the #players li.new rule in style.css shows them in the accent color, in bold.
  • Calling drawPlayers() again after Sam’s late yes showed seven names, not four: the first call’s three names were still there, and append() added the second call’s four after them. So drawPlayers() first clears the list with playerList.replaceChildren().
  • The rule: create it, fill it, append it. In a loop, one new element per pass.

The Final Code

Here’s where Priya’s two files end up after this video. Copy them if you want to compare your own work, or to start the next exercise from the same place.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Priya’s page now shows its date and host, filled in by main.js. Under Who’s Coming, though, the players list is empty.

00:09 Three people have said yes so far: Amara, Jonas and Wei. Jonas is coming for the first time. Add them to main.js as an array of records, each with a name and whether they’re new.

00:21 The list holds no items for your script to fill. So in this video, the script makes the items itself, one for each player.

00:31 Try it first in the Console of Priya’s page. The document’s createElement method makes a new element of the type you name. Ask for an li, a list item, and store it in a constant, item, for later.

00:44 Type item on its own line, and the Console shows an empty list item.

00:50 Look at the page, though. Nothing has changed. The new element exists only in JavaScript, held in item, until you put it on the page.

01:00 Set its text to Amara with textContent, as you did for the date line. Then find the players list and call its append method with the item. Amara appears under Who’s Coming.

01:12 In the Elements panel, a list item now sits inside the players list. Watch out for innerHTML, which many tutorials use here. It reads text as HTML, so a name a visitor typed could run as code.

01:27 Use textContent instead.

01:31 For every player, you need a loop. In main.js, find the players list and keep it in playerList, below the array. Here’s a first attempt many people write, and it’s a detour.

01:43 It creates one list item before the loop. Each pass sets its text to the next name and appends it to the list. Your turn: pause the video and predict how many items the list shows.

01:57 Save, and the list shows one item, Wei. Look at createElement. It’s above the loop, so it ran once and made one element. The first pass appends it. The second pass changes its text and appends it again.

02:13 An element can be in only one place, so append moves it, never copies it. It was already at the end, so there’s still one. The third pass does the same. So move createElement into the loop, and each pass makes its own item.

02:29 Save, and Amara, Jonas and Wei each get an item.

02:35 Jonas is new, and Priya wants first-timers to stand out. Add the rule shown to style.css. It gives a players list item with the class new the accent color, in bold.

02:47 Every element has a classList, the list of its classes. Inside the loop, if the player is new, call add on the item’s classList, with new as the class name.

02:58 Save, and Jonas shows in the accent color, in bold.

03:04 Then Sam says yes, late. To draw the list again, wrap the loop in a function called drawPlayers, and call it once. Save, and the list looks the same.

03:16 Now, at the end of main.js, push Sam onto the players array and call drawPlayers again. Later on, a button will do this. Save, and the list shows seven names, not four: Amara, Jonas and Wei twice, then Sam.

03:33 The first call’s three names are still there, and append adds the second call’s four after them. So clear the list first. The first line of drawPlayers calls replaceChildren on the list with nothing inside the parentheses, which empties it.

03:49 Save, and the list is back to four names, with Sam at the end.

03:55 So here’s the rule. To add an element, create it, fill it, and append it to an element on the page. In a loop, create a new one on every pass.

04:06 You’ve built Priya’s players list from her data. Next comes Exercise: List the Snacks, where you’ll build the list of who’s bringing what.

Become a Member to join the conversation.