-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdom_traversal.html
More file actions
149 lines (117 loc) · 7.79 KB
/
dom_traversal.html
File metadata and controls
149 lines (117 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DOM Traversal</title>
<style>
body {
margin: 0 auto;
}
div {
display: flex;
flex-wrap: wrap;
margin: .5rem;
padding: 1rem;
border: 3px solid black;
min-height: 1rem;
flex-grow: 1;
}
div h1 {
flex: 0 0 100%;
padding-left: 2rem;
}
.grandparent {
background-color: hsl(0, 50%, 50%);
flex-direction: column;
}
.parent {
background-color: hsl(100, 50%, 50%);
}
.child {
background-color: hsl(200, 50%, 50%);
}
</style>
</head>
<body>
<div>
<h1>9 techniques of DOM traversal</h1>
<div class="grandparent" id="grandparentid">
<div class="parent">
<div class="child" id="firstchild"></div>
<div class="child"></div>
</div>
<div class="parent">
<div class="child" id="thirdchild"></div>
<div class="child" id="lastchild"></div>
</div>
</div>
</div>
<script>
/* //************FIRST PART********
// selecting elements by ID
const grandParent = document.getElementById("grandparentid")
grandParent.style.border = "dotted"
// selecting elements by class name
// using Array.from() to make the collection returned by document.getElementsByClassName an array
const parents = Array.from(document.getElementsByClassName("parent"))
// iterate trrough all parent elements using forEach()
// parents.forEach(changeColor)
// selecting elements using querySelector()
// if selected element class or name appear more than once, querySelector only selecs the first one. To select all elements of same name or class , we use querySelectorAll()
const firstChild = document.querySelector("#firstchild")
changeColor(firstChild)
const grandParentTwo = document.querySelector(".grandparent")
changeColor(grandParentTwo)
// selecting all elements with same name or class name using querySelectorAll()
const parentsAgain = document.querySelectorAll(".parent")
parentsAgain.forEach(changeColor2)
*/
/*
//****************SECOND PART****
//SELECTING CHILDREN
const grandParent = document.querySelector(".grandparent")
const parents = Array.from(grandParent.children)
// parents.forEach(changeColor)
const parentOne = parents[0] //select first parent
changeColor(parentOne)
const children = parentOne.children
changeColor2(children[1])
*/
//**********THIRD PART********
//QUERY SELECTOR CAN BE USED ON ANY ELEMENT IN THE DOM
const grandParent = document.querySelector(".grandparent")
const children = grandParent.querySelectorAll(".child")
//children.forEach(changeColor2)
//SELECTING PARENTS
const lastChild = document.querySelector("#lastchild")
changeColor(lastChild)
const parent = lastChild.parentElement
changeColor(parent)
const grandParentEl = parent.parentElement
changeColor2(grandParentEl)
//SKIPPING PARENT
//Just like query selector selects the closest child passed in, clossest() selects the closest parent matching passed identity
const thirdChild = document.querySelector("#thirdchild")
const grandParentElement = thirdChild.closest(".grandparent")
changeColor3(grandParentElement)
// MOVING SIDE WAYS, NEXT AND PREVIOUS ELEMENTS
const firstChild = document.querySelector("#firstchild")
const secondChild = firstChild.nextElementSibling
changeColor3(secondChild)
const firstChildAgain = secondChild.previousElementSibling
changeColor2(firstChildAgain)
// BASE FUNCTIONS
function changeColor(element) {
element.style.backgroundColor = "white"
}
function changeColor2(element) {
element.style.backgroundColor = "black"
}
function changeColor3(element) {
element.style.backgroundColor = "yellow"
}
</script>
</body>
</html>