forked from priya42bagde/JavaScript-with-JC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutationObserver.html
More file actions
153 lines (127 loc) · 4.15 KB
/
Copy pathMutationObserver.html
File metadata and controls
153 lines (127 loc) · 4.15 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
150
151
152
153
<!-- 💡"JavaScript-with-JC"
👉 Mutation Observer
The MutationObserver API allows you to continuously monitor the changes being made to the DOM tree.
When the DOM nodes change, you can invoke a callback function to detect the changes.
-->
<!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>Mutation Observer</title>
<style>
body {
margin: 0;
background-color: powderblue;
padding: 30px;
}
.container {
display: flex;
flex-direction: column;
height: 100vh;
align-items: center;
justify-content: center;
text-align: center;
}
.item {
padding: 15px;
background-color: blanchedalmond;
list-style-type: none;
margin: 15px 0px;
width: 50vh;
font-size: 25px;
}
.btn {
background-color: red;
color: azure;
padding: 10px;
border: none;
border-radius: 10px;
font-size: 16px;
cursor: pointer;
margin-top: 10px;
}
.btn:active {
background-color: azure;
color: black
}
.box {
padding: 20px;
background-color: blanchedalmond;
list-style-type: none;
margin: 15px 0px;
width: 60vh;
font-size: 25px;
}
.colorBox {
padding: 20px;
background-color: blanchedalmond;
list-style-type: none;
margin: 15px 0px;
width: 60vh;
font-size: 25px;
height: 200px;
width: 200px;
display: grid;
place-items: center;
}
</style>
</head>
<body>
<div class="container">
<h1>#JavaScript-with-JC</h1>
<h1>Mutation Observer</h1>
<h1>💡Example 1:- Observing Changes in color of box</h1>
<div class="colorBox" contenteditable>Random Color Box</div>
<button class="btn" style="background-color: blue;" onclick="ChangeColor()">Change color of box</button>
</div>
<div class="container">
<h1>💡Example 2:- Observing Changes in List of Items </h1>
<ul id="itemsList">
<li class="item">First Item</li>
<li class="item">Second Item</li>
<li class="item">Third Item</li>
</ul>
<button class="btn" onclick="deleteFirstItem()">Delete First Item</button>
</div>
<div class="container">
<h1>💡Example 3:- Observing Changes in content of box</h1>
<div class="box" contenteditable>Editable Box, Try to edit</div>
</div>
<script>
// First example
const colorBox = document.querySelector(".colorBox")
const colorBoxObserver = new MutationObserver((mutations) => {
console.log("Change in Color Box", mutations);
})
colorBoxObserver.observe(colorBox, {
attributes: true
})
function ChangeColor() {
const randomColor = Math.floor(Math.random() * 16777215).toString(16);
colorBox.style.backgroundColor = "#" + randomColor;
}
// Second example
const itemsList = document.querySelector("#itemsList")
const observer = new MutationObserver((mutations) => {
console.log("Changes in Item List", mutations);
})
observer.observe(itemsList, {
childList: true
})
function deleteFirstItem() {
if (itemsList?.children?.length > 0) { itemsList.children[0].remove() }
}
// Third example
const box = document.querySelector(".box")
const boxObserver = new MutationObserver((mutations) => {
console.log("Change in Content of box", mutations);
})
boxObserver.observe(box.childNodes[0], {
characterData: true,
characterDataOldValue: true
})
</script>
</body>
</html>