Viewers: in countries Watching now:
As we saw in the previous chapter, Java Script has a number of DOM selection functions. However, once you have the element it's important to understand how to move up and down the node tree. Every node in the DOM has a series of properties that you can use to move up and down the node list. So for example, you can use parent node to go up a level and get to the parent of an existing node. Child node is an array of all the children of the current nodes. So any sub-nodes of the current node. You can use firstChild and lastChild as a property and acts as the first and last element of the children of the current node.
Finally, you can also use previousSibbling and nextSibbling to move within elements that have the same parent. So, let me show you how this works. Here's our document and I'm going to go into the developer tools, then hit the Esc key to get into the console. So, first off I'm going to assign a node and our DOM to a variable. So, I'll say variable my node is going to be equal to document. I'm going to use query selector and then I'll choose the class of artist list and find the first LI element. Remember a query selector chooses only the first element. So right now we're looking for this artist list right here and only the first list item that has this image.
Now it's really convenient when I can see the breadcrumbs on how to get to this element. So it's really useful to click right here on this magnifying glass and then click on an element. That shows me right here, bread crumbs to that element. So I can see that, there's an image inside this list item. And this is the list item that I have selected right here. So if I hit Enter. And I type in myNode, I can see this list item just the first one because this is document.querySelector. If you type in the dir command and you use myNode, you're no longer going to see the node as an XML or HTML file, you're going to see the node.
And then if you click on this triangle you'll be able to see all of the different properties available. So you can see things here like child nodes and other things like parent node, right here. So let's try accessing the parent of this node. So we'll say my node. That parent node. Now it's going to take us up one level to the node that has the entire list. Now, that's this level, right here. Of course, you can go the other way by accessing the child nodes. So we can say, get me the parent's nodes children by selecting my node.parentnode and then selecting child nodes.
Child nodes is an array like object that you can use to access elements inside the current node. Now you may notice something peculiar about these child nodes. Everything in the HTML document is a node including text. And you can see that when we check these nodes we have some text embedded into these elements. These are probably white based characters like carriage return and tabs that I've typed into my HTML document. They actually appeared as children of the parent node. You're probably expecting just a list items and that can sometimes be a problem.
And we can get to the first and last element in a node sequence because they have special keywords called firstchild and lastchild. So let's try to get the parent node that first child. And sure enough we're getting a text element not the first list item but the first text element. And the same way we can check for the parent nodes last child. And that gets us another text node. Probably a carriage return at the end of this list. Now you can navigate through different siblings, by using the next sibling and previous sibling. Let's try going to the parent node, and then we'll access the first child, and then we'll use next sibling.
And this is probably what you were expecting when you did first child, you got the second element. If you look at this array the first element is a text node, the second element is actually the list item. Of course, you can go the other way with previousSibling. Now you may use something like childNodes, siblings, firstChild and lastChild to loop through a series of elements. But the text node problem could be a pain. It would have probably been better to go back into our query selector and just choose Query Selector All. That would give us a list of only the list items. And we could loop through that array without having to worry about any text nodes.
So navigating through the different nodes with some of these node selectors may seem like a good way to go through the elements. But frankly it's a lot more trouble than it's worth. There's an easier way to navigate through these elements, but of course there's a cost. And I'll show you how to do that, in the next movie.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.