Understanding JavaScript DOM Traversal ๐Ÿ˜Ž

Understanding JavaScript DOM Traversal ๐Ÿ˜Ž

ยท

3 min read

Being able to select Elements and traverse through DOM is one of the most important skill that you can learn in JavaScript.

Let's Start

In the above embed you can see the HTML structure with one grandparent element, two parent elements and two child elements each in the parent to start with DOM traversing.

For selecting the grandparent element with or without id we will do :

querySelector which selects one single element with the id or if we pass a class name it will return the first element from the array of elements.

const grandparent = document.querySelector("#grandparent-id");


function changeColor(element) {
  element.style.backgroundColor = "#000"
}

//OR

//const grandparent = document.querySelector(".grandparent");

//const grandparent = document.getElementById("grandparent-id")

// function to change color of element to black
changeColor(grandparent);


// It will select the first element from the array of elements having parent class
//const parent = document.querySelector(".parent");

Screen Shot 2021-05-27 at 4.37.10 PM.png

For selecting parents

We will use querySelectorAll. This will select all the elements containing the class name.

const parents = document.querySelectorAll(".parent");

//OR
//const parents = Array.from(document.getElementsByClassName("parent"));

parents.forEach(changeColor);

Screen Shot 2021-05-27 at 4.44.59 PM.png

For selecting children of grandparent class

const grandparent = document.querySelector(".grandparent");

const parents = Array.from(grandparent.children)

parents.forEach(changeColor);

Screen Shot 2021-05-27 at 5.02.22 PM.png

For selecting children of parent class

Now we can select children of the parent element so we can select children of parent class.

const grandparent = document.querySelector(".grandparent");

const parents = Array.from(grandparent.children)

const parentOne = parents[0]
const children = parentOne.children

//Changing Color of the first child

changeColor(children[0])

Screen Shot 2021-05-27 at 5.08.20 PM.png

Also here we can directly fetch the children from the grandparent class

We can use querySelector and querySelectorAll not only on document by also on the elements to fetch children elements in that ๐Ÿ˜Ž

const grandparent = document.querySelector(".grandparent");

const childOne = grandparent.querySelector(".child")


//Changing Color of the first child

changeColor(childOne)

Screen Shot 2021-05-27 at 5.18.47 PM.png

For Selecting All Child Class Elements

const grandparent = document.querySelector(".grandparent");

const children = grandparent.querySelectorAll(".child")


//Changing Color of child class

children.forEach(changeColor)

Screen Shot 2021-05-27 at 5.22.31 PM.png

For Selecting All Parent Class Elements from Child Class

Let's provide an id to first child as child-one

const childOne = document.querySelector("#child-one");

const parent = childOne.parentElement


//Changing Color of childOne parent

changeColor(parent)

Screen Shot 2021-05-27 at 5.28.12 PM.png

Now we can select grandparent also

const childOne = document.querySelector("#child-one");

const parent = childOne.parentElement

const grandparent = parent.parentElement


//Changing Color of grandparent element fetched by parent element
changeColor(grandparent)

Screen Shot 2021-05-27 at 5.30.29 PM.png

Select grandparent Element directly from child element using closest method

closest is very similar to querySelector by moving upwards to fetch elements. It selects the closest parent element that has the selector which has been passed in the argument.

const childOne = document.querySelector("#child-one");

const grandparent = childOne.closest(".grandparent")


//Changing Color of grandparent element fetched by parent element
changeColor(grandparent)

Screen Shot 2021-05-27 at 5.39.13 PM.png

Now we know how to move up the tree and down the tree. Let's how to move sideways.

Selecting Sibling of childOne using nextElementSibling

const childOne = document.querySelector("#child-one");

const childTwo = childOne.nextElementSibling

changeColor(childTwo)

Screen Shot 2021-05-27 at 5.44.30 PM.png

Selecting Previous Sibling of childTwo using previousElementSibling

const childOne = document.querySelector("#child-one");

const childTwo = childOne.nextElementSibling

changeColor(childTwo.previousElementSibling)

Screen Shot 2021-05-27 at 5.46.26 PM.png

That's It For Now. See You In Next One โœŒ๏ธ

ย