-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfour_parent_child.html
45 lines (40 loc) · 1.55 KB
/
four_parent_child.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body style="background-color: #212121; color: #fff;">
<div class="parent">
<div class="day">Monday</div>
<div class="day">Tuesday</div>
<div class="day">Wednesday</div>
<div class="day">Thrusday</div>
</div>
</body>
<script>
// Note: See some changes and console logs on browser's console.
// Selecting Parent element
const parent = document.querySelector('.parent')
// Using querySelector cause there is one parent class element.
console.log(parent);
console.log(parent.children); // Gives HTMLCollection
// Accessing elements
console.log(parent.children[1].innerHTML); // Tuesday
// For Loop on HTMLCollection
for (let i = 0; i < parent.children.length; i++) {
console.log(parent.children[i].innerHTML);
}
// Styling on Children
parent.children[1].style.color = 'orange'
console.log(parent.firstElementChild); // Gives the first child element.
console.log(parent.lastElementChild); // Gives the last child element.
// From child to parent
const dayOne = document.querySelector('.day')
console.log(dayOne); // Selects the first element
console.log(dayOne.parentElement); // Selects parent element
console.log(dayOne.nextElementSibling); // Selects next element to the first element.
console.log("Nodes: ", parent.childNodes); // Gives the Nodelist
</script>
</html>