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
€92.78
List Price:  €129.90
You save:  €37.11
£79.40
List Price:  £111.16
You save:  £31.76
CA$136.11
List Price:  CA$190.56
You save:  CA$54.44
A$154.13
List Price:  A$215.78
You save:  A$61.65
S$135.08
List Price:  S$189.12
You save:  S$54.03
HK$782.28
List Price:  HK$1,095.23
You save:  HK$312.94
CHF 90.61
List Price:  CHF 126.85
You save:  CHF 36.24
NOK kr1,085.23
List Price:  NOK kr1,519.37
You save:  NOK kr434.13
DKK kr692.01
List Price:  DKK kr968.84
You save:  DKK kr276.83
NZ$167.80
List Price:  NZ$234.94
You save:  NZ$67.13
د.إ367.19
List Price:  د.إ514.08
You save:  د.إ146.89
৳10,976.08
List Price:  ৳15,366.96
You save:  ৳4,390.87
₹8,339.52
List Price:  ₹11,675.66
You save:  ₹3,336.14
RM473.25
List Price:  RM662.57
You save:  RM189.32
₦141,842.81
List Price:  ₦198,585.61
You save:  ₦56,742.80
₨27,810.04
List Price:  ₨38,935.18
You save:  ₨11,125.13
฿3,647.70
List Price:  ฿5,106.92
You save:  ฿1,459.22
₺3,232.12
List Price:  ₺4,525.11
You save:  ₺1,292.98
B$499.21
List Price:  B$698.91
You save:  B$199.70
R1,908.54
List Price:  R2,672.04
You save:  R763.49
Лв180.65
List Price:  Лв252.92
You save:  Лв72.26
₩135,197.71
List Price:  ₩189,282.20
You save:  ₩54,084.49
₪368.63
List Price:  ₪516.10
You save:  ₪147.47
₱5,633.91
List Price:  ₱7,887.71
You save:  ₱2,253.79
¥15,144.47
List Price:  ¥21,202.86
You save:  ¥6,058.39
MX$1,659.40
List Price:  MX$2,323.22
You save:  MX$663.82
QR364.31
List Price:  QR510.04
You save:  QR145.73
P1,370.91
List Price:  P1,919.33
You save:  P548.42
KSh13,148.68
List Price:  KSh18,408.68
You save:  KSh5,260
E£4,729.52
List Price:  E£6,621.52
You save:  E£1,892
ብር5,680.63
List Price:  ብር7,953.11
You save:  ብር2,272.48
Kz83,612.74
List Price:  Kz117,061.18
You save:  Kz33,448.44
CLP$97,978.20
List Price:  CLP$137,173.40
You save:  CLP$39,195.20
CN¥722.95
List Price:  CN¥1,012.16
You save:  CN¥289.21
RD$5,921.50
List Price:  RD$8,290.34
You save:  RD$2,368.83
DA13,490.83
List Price:  DA18,887.70
You save:  DA5,396.87
FJ$226.12
List Price:  FJ$316.58
You save:  FJ$90.46
Q779.86
List Price:  Q1,091.83
You save:  Q311.97
GY$20,923.51
List Price:  GY$29,293.76
You save:  GY$8,370.24
ISK kr13,946.60
List Price:  ISK kr19,525.80
You save:  ISK kr5,579.20
DH1,013.19
List Price:  DH1,418.51
You save:  DH405.32
L1,763.34
List Price:  L2,468.75
You save:  L705.40
ден5,702.11
List Price:  ден7,983.18
You save:  ден2,281.07
MOP$805.89
List Price:  MOP$1,128.28
You save:  MOP$322.39
N$1,893.44
List Price:  N$2,650.90
You save:  N$757.45
C$3,681.15
List Price:  C$5,153.75
You save:  C$1,472.60
रु13,335.63
List Price:  रु18,670.42
You save:  रु5,334.78
S/370.84
List Price:  S/519.19
You save:  S/148.35
K382.72
List Price:  K535.82
You save:  K153.10
SAR375
List Price:  SAR525.01
You save:  SAR150.01
ZK2,522.76
List Price:  ZK3,531.96
You save:  ZK1,009.20
L461.43
List Price:  L646.02
You save:  L184.59
Kč2,350.75
List Price:  Kč3,291.15
You save:  Kč940.39
Ft36,729.02
List Price:  Ft51,422.10
You save:  Ft14,693.08
SEK kr1,071.30
List Price:  SEK kr1,499.86
You save:  SEK kr428.56
ARS$85,766.82
List Price:  ARS$120,076.98
You save:  ARS$34,310.16
Bs691.04
List Price:  Bs967.48
You save:  Bs276.44
COP$387,583.68
List Price:  COP$542,632.66
You save:  COP$155,048.97
₡50,832.34
List Price:  ₡71,167.31
You save:  ₡20,334.97
L2,468.78
List Price:  L3,456.40
You save:  L987.61
₲737,805.73
List Price:  ₲1,032,957.54
You save:  ₲295,151.80
$U3,781.90
List Price:  $U5,294.82
You save:  $U1,512.91
zł400.73
List Price:  zł561.05
You save:  zł160.31
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.