Traversing the DOM

Modern JavaScript Manipulating HTML Pages
15 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€96.21
List Price:  €134.70
You save:  €38.49
£79.93
List Price:  £111.90
You save:  £31.97
CA$143.79
List Price:  CA$201.32
You save:  CA$57.52
A$160.48
List Price:  A$224.69
You save:  A$64.20
S$135.83
List Price:  S$190.17
You save:  S$54.33
HK$777.07
List Price:  HK$1,087.93
You save:  HK$310.86
CHF 89.45
List Price:  CHF 125.24
You save:  CHF 35.78
NOK kr1,141.95
List Price:  NOK kr1,598.78
You save:  NOK kr456.82
DKK kr717.64
List Price:  DKK kr1,004.72
You save:  DKK kr287.08
NZ$177.52
List Price:  NZ$248.53
You save:  NZ$71.01
د.إ367.25
List Price:  د.إ514.17
You save:  د.إ146.91
৳11,994.74
List Price:  ৳16,793.12
You save:  ৳4,798.37
₹8,496.63
List Price:  ₹11,895.62
You save:  ₹3,398.99
RM450.75
List Price:  RM631.07
You save:  RM180.32
₦155,628.43
List Price:  ₦217,886.03
You save:  ₦62,257.60
₨27,934.96
List Price:  ₨39,110.06
You save:  ₨11,175.10
฿3,444.45
List Price:  ฿4,822.37
You save:  ฿1,377.92
₺3,518.54
List Price:  ₺4,926.10
You save:  ₺1,407.55
B$617.53
List Price:  B$864.57
You save:  B$247.04
R1,837.80
List Price:  R2,572.99
You save:  R735.19
Лв188.31
List Price:  Лв263.64
You save:  Лв75.33
₩144,872.25
List Price:  ₩202,826.95
You save:  ₩57,954.69
₪364.89
List Price:  ₪510.86
You save:  ₪145.97
₱5,883.01
List Price:  ₱8,236.45
You save:  ₱2,353.44
¥15,672.80
List Price:  ¥21,942.55
You save:  ¥6,269.75
MX$2,028.91
List Price:  MX$2,840.56
You save:  MX$811.64
QR366.34
List Price:  QR512.89
You save:  QR146.55
P1,382.59
List Price:  P1,935.69
You save:  P553.09
KSh12,923.70
List Price:  KSh18,093.70
You save:  KSh5,170
E£5,091.27
List Price:  E£7,127.98
You save:  E£2,036.71
ብር12,764.56
List Price:  ብር17,870.89
You save:  ብር5,106.33
Kz91,790.82
List Price:  Kz128,510.82
You save:  Kz36,720
CLP$99,155.08
List Price:  CLP$138,821.08
You save:  CLP$39,666
CN¥729.81
List Price:  CN¥1,021.77
You save:  CN¥291.95
RD$6,104.52
List Price:  RD$8,546.58
You save:  RD$2,442.05
DA13,454.60
List Price:  DA18,836.98
You save:  DA5,382.37
FJ$232.11
List Price:  FJ$324.96
You save:  FJ$92.85
Q773.21
List Price:  Q1,082.52
You save:  Q309.31
GY$21,000.46
List Price:  GY$29,401.48
You save:  GY$8,401.02
ISK kr13,903.60
List Price:  ISK kr19,465.60
You save:  ISK kr5,562
DH1,007.40
List Price:  DH1,410.40
You save:  DH403
L1,841.78
List Price:  L2,578.57
You save:  L736.78
ден5,922.50
List Price:  ден8,291.74
You save:  ден2,369.23
MOP$803.42
List Price:  MOP$1,124.82
You save:  MOP$321.40
N$1,834.70
List Price:  N$2,568.66
You save:  N$733.95
C$3,694.32
List Price:  C$5,172.20
You save:  C$1,477.87
रु13,665.58
List Price:  रु19,132.35
You save:  रु5,466.77
S/374.71
List Price:  S/524.60
You save:  S/149.89
K406.86
List Price:  K569.63
You save:  K162.76
SAR375.72
List Price:  SAR526.02
You save:  SAR150.30
ZK2,777.96
List Price:  ZK3,889.25
You save:  ZK1,111.29
L478.81
List Price:  L670.35
You save:  L191.54
Kč2,418.25
List Price:  Kč3,385.65
You save:  Kč967.40
Ft39,800.47
List Price:  Ft55,722.25
You save:  Ft15,921.78
SEK kr1,103.66
List Price:  SEK kr1,545.17
You save:  SEK kr441.50
ARS$102,190.76
List Price:  ARS$143,071.15
You save:  ARS$40,880.39
Bs693.61
List Price:  Bs971.08
You save:  Bs277.47
COP$437,799.12
List Price:  COP$612,936.28
You save:  COP$175,137.16
₡50,480.33
List Price:  ₡70,674.48
You save:  ₡20,194.15
L2,548.03
List Price:  L3,567.35
You save:  L1,019.31
₲784,126.06
List Price:  ₲1,097,807.85
You save:  ₲313,681.79
$U4,460.41
List Price:  $U6,244.75
You save:  $U1,784.34
zł409.96
List Price:  zł573.96
You save:  zł164
Already have an account? Log In

Transcript

There are times that once you've retrieved a node from the DOM that you need to be able to traverse the DOM from that node. In other words, move to a different node, a parent node, a child node. In this video, we're going to look at the JavaScript commands that allow you to do that. So first, let's introduce those commands. First we have parent node. So if you want to get the parent of the current node you have parent node will allow you to do that child nodes that will return an array of all the child nodes of the current node you have.

First child will return the first child, last child will return the last child. Then you have next sibling and previous sibling. So those two commands allow you to traverse on the same level. So for example, if I jump out To the HTML page, one that we've been using as an example, for the last several videos, these Li tags are on the same level. Therefore, these are siblings of each other. So if I had this node, next sibling would go to this node, and so on.

Now let's take a look at how we would use these. So to do that, I'm going to move to our selecting exercise that we've been using. I'm then going to open up the console. And first, let me show you the structure again. So I've come into the console got gone to the elements tab, and inside the body, we have a div tag with an ID of content. If I open up that div tag, there's an h1 tag, there's another div tag for the bullets, and then the bullets are inside of that.

Okay, so that's our structure. We can see that same thing in the HTML file. Here's that div tag with the ID have content. Inside Out, we have an h1 and then another div tag, and then the bullets are inside of that. So I'm going to select this first div tag, and we'll move it around the DOM using that div tag. So to do that, I simply enter var node equal to document dot query selector.

And then since I'm using query selector, I indicate that I want the ID of content. The hash symbol indicates that I'm looking for an ID press return. Now if I display node, we can see that we have that div tag. If we open it up, we can see everything that's inside it. Alright, now let's find out what the parent node of that is. So if I type notes A parent node, it returns the body tag.

And that makes sense. If we look at the HTML file, here's that div tag. Well, its parent would be body. This is a sibling. And the footer is a sibling as well. So let's try to find it sibling.

Let's do next sibling. Oh, no, what's this? What do we get here? That does not look like the footer tag that we were looking at. We have some sort of text node. So what why are we getting that as the next sibling?

Well, to illustrate that, let me display all the child nodes of this. Remember, that returns an array Sure enough, I have the h1 now I also have the div of bullets that contain all the bullets, but I have some other text nodes as well. What are these text nodes? Well, that's an important thing to understand is you're trying to traverse the DOM. So everything in an HTML document is converted to a node. And that includes text.

So as it's going through and converting stuff as it as it finds a tag, it converts that entire tag to a node. But there is a carriage return that follows that tag, or there may be a tab that follows that tag that is converted to a text node. And that's what we're seeing here. For example, if I open this up in the data, it looks like it's telling me that that's probably a carriage return that it has encountered there. So all of those text pieces that exist within an HTML file, become nodes as well. Not only that, HTML elements, but the text pieces.

So that makes it a little bit tricky for us to traverse the DOM. See if I were to use node dot first child. Well, that's great, except it doesn't give me what I want. I was hoping to get the h1 where I get the text element. So how do we deal with that? Well, let me jump back.

And we're going to introduce node type, node name, node value and attributes. Those help us work with this issue. So let me introduce those quickly. So if we check the node type of a node, it will return a numeric type it will return a number which indicates what type of node that is. That can help us separate between nodes that are elements of the HTML document and text nodes. For example, the common node types you can get is one as an element.

So if you if it returns the number one, you know that is an element. If it returns a two, it's an attribute of that node. If it returns a three, it is a text element. So no type can be very helpful. Node name can also be helpful because no name will return an upper case tag name for that particular nodes. So if it's a node that was created from a tag, it would display a tag when we tested the node name.

Now node value also can be used and we'll look at that in a lot more detail when we start looking at changing values, but basically node value will return no for a regular element. Or it can return the text from a text node. Then finally, attributes returns an array of attributes So the attributes that are assigned to that particular node. So let's take a look at how that can help us. I'm going to select the first bullet on this HTML page. I'll use get element by ID this time.

And the ID for that is B one. We go ahead and press return. All right, let's take a look at B one. Just make sure we got what we're after. And yes, that looks like that's the first bullet. Let me dock this so it'll be a little bit easier to see.

And we can see that the contents of that using different slack methods. Alright, let's check the node name for that. Be one dot node name and that comes back With the tag in uppercase, let's see what the node type is. It is a one. This is an HTML element and therefore the node type is one. Now, does this have child nodes?

Well, let's check. So it returns an array and look what's in the array. It looks like there is one element and it happens to be the contents of this. Now this is an important concept. When you have a node that is derived from an HTML element, its child node is a text node that contains the contents of that tag. So to illustrate that, let me type be one child node And then I'm going to get the first element in that array, because there is only one.

And then let's check the node type of that. Ah, it's a three. That means it's a text node. Now also, let's take a look at one node value. What does that return that returns a no. So node value will return a no whenever it is an HTML element.

But what if we go child nodes? Get the first one again. And now do node value. Ah, look at that. That's the actual text. Now this is how we would go about changing the text of that.

No code is we have to get the child nodes of it to get that text node. And then we can get the node value and we can change the node value or display the node value. So let me execute that again. But this time, I'm going to set the node value equal to something else. Sure enough, when I press return, that first bullet changes to the new text which I assigned to it. All right, one more quick exercise to show how this can be helpful.

The node type, node name, etc. How it can be helpful when you're traversing nodes and trying to work with the DOM. Let's say I wanted to select all of these Li tags, the nodes created for all of these Li tags, and I wanted to change go through each one and change the color to green. How would I go about doing that? Well, I could select each one individually. And then change it because we have an idea attached to it.

What if we did not have an ID attached to it? What would we do them? Well, here's one example. First, I'm going to gather those up. Using this statement here, create a variable nodes and set it equal to document query selector. I just want to find the first one.

And then inside the query selector, I have dot bullets, which is the class bullets. And then the descendant of that is the oil tag. So that will get all of them because it will get the oil tag and they are all children of that tag. I press return. We can take a look at that. Sure enough, that's the ol tag.

Now, I'm going to create an other variable really quick. I want to get the child nodes of that press return. What does it give me? See, I've got all of the individual Li, Li tags, but notice I have some text nodes as well. So I have the nodes associated with the Li tag, but I have text nodes as well. And so I need to be able to traverse through this making a change to the style, but not getting these text nodes.

So let's create a for loop that would do that. Now so you don't have to see me type all this in. I've got this already set up and let's just walk through it. So in the for loop, I let i equals zero, and then we want to run the loop while I is less than the length, the length of this array because you all remember child nodes returns an array. And so it is an array of nodes. And so we're going to go through that accessing each node within that array, and then I increment it.

Now I check, if you LS i. So if the first time through this loop, if the first node has a node type of one, then we're going to change the style. We do that right here dot style dot color equals green. Remember, no type of one is an HTML element. No type of two are attributes. No type of three is a text node.

All right, let's see how that works. So I press return. Sure enough, it changes them all to green. I didn't get any error messages because we have this if conditional which checks to make sure that is the right type of node before I actually try to change the style. If I had tried to change the style on a text node, I would get an error because a text node does not have a style property. So we've covered multiple things.

Here we've covered JavaScript commands that allow us to traverse the node, parent node, child nodes, which returns an array of children nodes, first child, last child, previous sibling, next sibling. Then we also discussed no type, node name, node value, and attribute and how those can help us as we're working with node. Now with newer browsers, there is an easier way to make sure you're only selecting HTML elements when you're working with nodes. We'll take a look at In the next video

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.