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

Step Through the Page Tree

In this video, you started from Jonas’s Check in button and stepped through the page’s tree to the elements around it, then gave the Check in handler the same step up to its guest’s list item.

Key Points

  • Suppose all you hold is a Check in button. To change a guest’s whole row later, the code has to reach the list item around the button.
  • Each player’s list item is a child of the players list, and it has two children of its own: the span with the name, and the button.
  • Every element knows where it sits in the tree. parentElement is the element directly around it, so jonasButton.parentElement is Jonas’s list item.
  • children is a list of the elements directly inside an element, in page order. This list is an HTMLCollection. Like a NodeList, it has a length and positions, and for...of works on it, but it has no forEach().
  • firstElementChild gives only the first of them. On Jonas’s item, that’s the span, and its textContent is 'Jonas'.
  • Watch out for firstChild and childNodes, without Element in their names: they count the text between tags as well. Jonas’s item, built with createElement() and append(), has none. But in index.html, the line break before the first heading in main is text, so after const main = document.querySelector("main");, main.firstChild is that text, not the heading. main.firstElementChild is the heading.
  • nextElementSibling steps across to the next element with the same parent. From Jonas’s span, that’s his button.
  • Each parentElement is one step up, so jonasButton.parentElement.parentElement is the players list, <ul id="players">.
  • Counting steps depends on the exact HTML. closest() takes a CSS selector, checks the element itself, then its parent, then each one further up, and returns the first match. jonasButton.closest("li") is Jonas’s list item. If nothing matches, null comes back.
  • From an element you hold, parentElement and closest() step up, children and firstElementChild step down, and nextElementSibling steps across.

The Final Code

Here’s drawPlayers() in scratch/main.js after this video. Only the Check in arrow’s body changed:

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 In Find Every Matching Element, your loop went down from each list item to its button. A Check in handler starts from the other end. Suppose all you hold is the button.

00:10 In this video, you’ll step from that button to the elements around it. If you don’t have Priya’s files, the text below has a version for a blank tab. Later in this section, Priya wants a guest’s whole row marked when they check in, not only the button.

00:25 So from Jonas’s button, the code has to reach the list item around it. Reload the page to clear the last lesson’s labels. In the Console, ask querySelectorAll for the buttons in the players list, and keep the second one, Jonas’s, in a const called jonasButton.

00:45 You’ve seen the page as a tree in the Elements panel, where an element holds its children. Here’s Jonas’s corner of it. His list item is a child of the players list, and it has two children of its own: the span with his name, and the button.

01:00 Every element knows where it sits in that tree. Its parentElement property is the element directly around it. Type jonasButton.parentElement, and the Console shows Jonas’s list item, with its class new.

01:14 That’s the step the handler needs. With one property, the button reaches its row without searching the page again.

01:23 Going down works too. An element’s children property is a list of the elements directly inside it, in page order. On Jonas’s item, the Console shows two: the span and the button.

01:35 This list is an HTMLCollection. Like a NodeList, it has a length and positions, and for…of works, but forEach() doesn’t. firstElementChild gives only the first of them.

01:48 On Jonas’s item, that’s the span, and its textContent is Jonas.

01:54 Watch out for firstChild and childNodes, without Element in their names: they count the text between tags as well. Jonas’s item has none, but in index.html, the line break before main’s first heading is text, so main.firstChild gives that text.

02:12 From the span, nextElementSibling steps across to the next element with the same parent. That’s the button you started from.

02:22 Here’s a quick check. Pause the video and predict what jonasButton.parentElement.parentElement gives.

02:33 It’s the players list, the ul with the id players.

02:39 Each parentElement is one step up. Counting steps works, but it depends on the exact HTML. If Priya ever wrapped each button in another element, one step up would land on the wrapper instead of the row.

02:53 So the closest method lets you name what you want. It takes a CSS selector, checks the element itself, then its parent, then each one further up, and returns the first match.

03:05 jonasButton.closest(“li”) gives Jonas’s list item, the same one parentElement gave. If nothing matches, null comes back.

03:16 Now put it in the handler. In drawPlayers, the Check in arrow has the button from its own pass of the loop. Inside the arrow, a const called playerItem holds button.closest(“li”), the guest’s list item.

03:29 Then log the name in its span. Save and reload the page, then click Jonas’s Check in button. The Console shows Checked in: Jonas, read from the page.

03:41 The arrow could use that pass’s item instead. But stepping from the button works anywhere you hold only the button, as in the Console a moment ago. Later on the path, you’ll write handlers that only know which element was clicked.

03:54 That’s all you need about those handlers for now.

03:58 So here’s the rule. From an element you hold, parentElement and closest step up. The children and firstElementChild properties step down, and nextElementSibling steps across.

04:11 Jonas’s button can now reach his row. Next, in Read and Set Attributes and Data, you’ll read and set more on elements like that row, starting with which player each one belongs to.

Become a Member to join the conversation.