Jonas checked in tonight and then had to leave early. In this video, you gave every row a Remove button, took Jonas’s row off the page, moved arrived guests to the top, made a copy of a row, and saw the arrivals count go wrong once a checked-in row was gone. Here are the key points:
- An element sits in one place in the page’s tree at a time. Everything below follows from that.
remove()takes an element out of the tree, together with everything inside it. Called on Jonas’s row, it takes his name and both buttons with it. If you pressed Remove from the keyboard, focus leaves with the button and falls back to the page. Putting focus somewhere sensible comes later.prepend()puts an element first inside another one. Called on the players list with Wei’s row, which was already third, it moves the row to the top. The list still shows one Wei row, because inserting an element that’s already on the page takes it out of its old place first.- The other insert methods move an element the same way. Take
amaraandsamas the Amara and Sam rows, for exampledocument.querySelector('[data-player-id="amara"]').closest("li"), on the page as Wei, Amara, Jonas, Sam.playerList.append(amara)puts Amara’s row last inside the list. - With
before()andafter(), the element you pass in is the one that moves. The one you call the method on stays put:amara.before(sam)gives Wei, Sam, Amara, Jonas, andamara.after(sam)gives Wei, Amara, Sam, Jonas. replaceWith()puts the element you pass in where the other one was, which takes that one off the page:amara.replaceWith(sam)leaves Wei, Sam and Jonas.- To have an element in two places, copy it with
cloneNode(true). Thetrueasks for a deep copy, with everything inside. Without it, you get the list item alone, empty. - A copy doesn’t get listeners added with
addEventListener(). Priya’s rows don’t need their own, because the list’s one listener handles the copy’s Remove Wei like any other row. - Changing the page doesn’t change your data. A reload draws the list from
playersagain, so Jonas’s row comes back. After Jonas checks in and leaves, the Console’s total still counts him. It saysArrived so far: 4, but only three people are there (Wei, his guest Lin, and Sam). The bar counts green rows overplayers.length, which still includes Jonas, so it stays at half when Sam checks in. - Change one element when the change stays inside one row. When a change touches totals or other rows, it’s often better to have your code draw the list again from the data. That’s what the next section does.
Here are the changes, in two places in main.js. In drawPlayers(), add the Remove button right after the line that appends the Check in button:
const removeButton = document.createElement("button");
removeButton.textContent = "Remove " + player.name;
removeButton.classList.add("remove");
item.append(removeButton);
In the list’s listener, add the remove branch at the top of the arrow, and the prepend() line after the line that adds arrived. The rest of the listener is unchanged: