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 forinnerHTML, which many tutorials use: it reads text as HTML, so a name a visitor typed could run as code. UsetextContentinstead. - 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, soappend()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. classListis an element’s list of classes.item.classList.add("new")gives a first-timer the classnew, and the#players li.newrule instyle.cssshows 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, andappend()added the second call’s four after them. SodrawPlayers()first clears the list withplayerList.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.