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

Move and Remove Elements

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 amara and sam as the Amara and Sam rows, for example document.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() and after(), 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, and amara.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). The true asks 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 players again, so Jonas’s row comes back. After Jonas checks in and leaves, the Console’s total still counts him. It says Arrived so far: 4, but only three people are there (Wei, his guest Lin, and Sam). The bar counts green rows over players.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:

Language: JavaScript
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:

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 list now has one Check in listener, on the list itself, for every row. Jonas checks in tonight, then has to leave early, so each row needs a Remove button.

00:11 In drawPlayers, after the Check in button, build a second button. Its text is Remove and the guest’s name, and it gets a remove class.

00:21 It has no player id, so the Check in code ignores it.

00:26 At the top of the list listener’s arrow function, use closest to look for a Remove button. If there is one, step up to its row and remove it.

00:35 Then return, so the Check in code doesn’t run. Save, reload, and click Check in Jonas. His row turns green, and the Console counts one arrival. Click Remove Jonas.

00:48 His row leaves the page.

00:51 The remove method takes an element out of the tree, with everything inside it.

00:57 If you pressed Remove from the keyboard, focus leaves with the button and falls back to the page. Putting focus somewhere sensible comes later.

01:07 Priya wants arrived guests at the top. After the line that adds the arrived class, call prepend on the players list with the row. The prepend method puts an element first inside another.

01:19 Save and reload, which draws the list from players again, so Jonas is back. Wei’s row is third. Your turn: pause the video and predict how many Wei rows the list shows after you click Check in Wei.

01:37 Click Check in Wei. One. Wei’s row is now first. An element can be in only one place in the tree, so the browser took Wei’s row out of its old place first. Inserting it moves it.

01:54 Other insert methods move rows too. These diagrams show each call’s result, with amara and sam as the Amara and Sam rows. Each starts from the page’s order, and your page doesn’t change.

02:06 The append method puts Amara’s row last inside the list. With before, the element you pass in moves, and the one you call it on stays put.

02:16 So Sam lands right before Amara. With after, Sam lands right after Amara. And replaceWith puts Sam in Amara’s place, taking Amara out.

02:31 Priya doesn’t need a second Wei row. This is only a look. In the Console, take the list’s first row. Then call its cloneNode method with true, and put the copy after the row.

02:43 The Console shows undefined for each line because neither gives back a value. A second Wei row appears, green, with both buttons. True asks for a deep copy, the row with everything inside it.

02:57 Without true, you’d get the list item alone, empty.

03:02 Listeners added with addEventListener don’t come along with a copy. Here that doesn’t matter. Click the copy’s Remove Wei, and the list’s one listener removes it.

03:14 Now check Jonas in, and his row moves to the top. The Console says three have arrived, counting Wei’s guest Lin, and the bar sits at half. Then remove him. Nothing else changes.

03:27 Your turn: pause the video and predict whether the bar grows when you click Check in Sam.

03:38 It doesn’t. The bar counts green rows. Jonas’s green row left, and Sam’s turned green, so it still counts two. And it still divides by four guests, Jonas included.

03:52 The Console’s total says four, but only three people are here. Removing the row changed the page, but not the arrived total or the players array.

04:02 The next section fixes that.

04:05 So an element lives in one place in the tree. Removing it takes it out, inserting it moves it, and a second place needs a copy. Changing one element suits a change that stays inside one row.

04:19 When a change touches totals or other rows, it’s often better to draw the list again from the data. Next, in Exercise: Tidy the Snack Table, you’ll let Priya remove snacks from her list and move one to the top.

Become a Member to join the conversation.