Start learning with our library of video tutorials taught by experts. Get started

JavaScript: Enhancing the DOM

Traversing up and down DOM nodes


From:

JavaScript: Enhancing the DOM

with Ray Villalobos

Video: 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.
Expand all | Collapse all
  1. 2m 36s
    1. Welcome
      59s
    2. What you should know before watching this course
      50s
    3. Using the exercise files
      47s
  2. 24m 33s
    1. What is the Document Object Model (DOM)?
      3m 2s
    2. Navigating the DOM with developer tools
      8m 10s
    3. Testing JavaScript commands with the console
      5m 50s
    4. Communicating with the console through JavaScript
      7m 31s
  3. 31m 9s
    1. Selecting elements with getElementById
      4m 10s
    2. Choosing elements by HTML tag
      3m 20s
    3. Isolating elements by class name
      3m 12s
    4. Querying CSS to select elements
      4m 54s
    5. Working with named form elements
      3m 39s
    6. Understanding nodeType, nodeName, and nodeValue
      4m 30s
    7. Traversing up and down DOM nodes
      4m 40s
    8. Targeting node elements
      2m 44s
  4. 22m 25s
    1. Changing HTML attributes
      5m 25s
    2. Working with restricted attributes
      2m 49s
    3. Detecting data attributes
      3m 29s
    4. Controlling classes with the HTML5 classList
      3m 21s
    5. Targeting the attributes property
      1m 24s
    6. Using text content modifiers
      3m 42s
    7. Modifying elements as text
      2m 15s
  5. 14m 57s
    1. Creating and appending nodes
      4m 27s
    2. Controlling node insertions with insertBefore
      3m 17s
    3. Cloning and removing nodes
      4m 41s
    4. Replacing existing nodes
      2m 32s
  6. 26m 14s
    1. What we'll build
      2m 16s
    2. Adding a bubbling event listener
      4m 11s
    3. Creating and styling an overlay with JavaScript
      4m 39s
    4. Adding an image
      3m 48s
    5. Resizing images in the DOM
      2m 59s
    6. Centering an image
      2m 36s
    7. Handling clicks
      1m 29s
    8. Adjusting for scrolling
      1m 36s
    9. Detecting and adjusting for a window resize
      2m 40s
  7. 1m 49s
    1. Next steps
      1m 49s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
JavaScript: Enhancing the DOM
2h 3m Intermediate Jun 10, 2013

Viewers: in countries Watching now:

The Document Object Model (DOM) is at the core of every HTML page. In order to develop dynamic HTML pages, a front-end developer needs to understand how JavaScript connects to and controls the DOM, allowing you to create, modify, delete, and edit existing page content. This course focuses on helping you understand the DOM elements, and shows the different ways JavaScript gives you access to them and makes it easier to work with the DOM. Author Ray Villalobos covers navigating the DOM, selecting elements, modifying HTML attributes, editing nodes, and much more.

Topics include:
  • What is the DOM?
  • Choosing and isolating elements
  • Traversing up and down DOM nodes
  • Changing HTML attributes
  • Modifying elements as text
  • Creating and appending nodes
  • Cloning and removing nodes
  • Adding a bubbling event listener
  • Adding and resizing images
  • Handling clicks
Subjects:
Developer Web Web Design Web Development
Software:
HTML JavaScript
Author:
Ray Villalobos

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 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.

There are currently no FAQs about JavaScript: Enhancing the DOM.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript: Enhancing the DOM.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked