From the course: JavaScript: Enhancing the DOM (2013)

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Traversing up and down DOM nodes

Traversing up and down DOM nodes

From the course: JavaScript: Enhancing the DOM (2013)

Start my 1-month free trial

Traversing up and down DOM nodes

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…

Contents