Repository navigation
Expand file tree
/
Copy pathindex.md
More file actions
150 lines (110 loc) · 4 KB
/
Copy pathindex.md
File metadata and controls
150 lines (110 loc) · 4 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
---
title: "MutationObserver: MutationObserver() constructor"
short-title: MutationObserver()
slug: Web/API/MutationObserver/MutationObserver
page-type: web-api-constructor
browser-compat: api.MutationObserver.MutationObserver
---
{{APIRef("DOM WHATWG")}}
The DOM **`MutationObserver()`**
constructor — part of the {{domxref("MutationObserver")}} interface — creates and
returns a new observer which invokes a specified callback when DOM events
occur.
DOM observation does not begin immediately; the
{{domxref("MutationObserver.observe", "observe()")}} method must be called first to
establish which portion of the DOM to watch and what kinds of changes to watch for.
## Syntax
```js-nolint
new MutationObserver(callback)
```
### Parameters
- `callback`
- : A function which will be called on each DOM change that qualifies given the
observed node or subtree and options.
The `callback` function takes as input two parameters:
1. An array of {{domxref("MutationRecord")}} objects, describing each change that
occurred.
2. The {{domxref("MutationObserver")}} which invoked the
`callback`. This is most often used to disconnect the observer using {{domxref("MutationObserver.disconnect()")}}.
See the [examples](#examples) below for more details.
### Return value
A new {{domxref("MutationObserver")}} object, configured to call the specified
`callback` when DOM mutations occur.
## Examples
### Observing child elements
This example has buttons to add an {{htmlelement("li")}} element to a list, and to remove the first `<li>` element from the list.
We use a `MutationObserver` to be notified about changes to the list. In the callback, we log additions and removals, and as soon as the list is empty, we disconnect the observer.
The "Reset example" button resets the example to its original state.
#### HTML
```html
<button id="add">Add child</button>
<button id="remove">Remove child</button>
<button id="reset">Reset example</button>
<ul id="container"></ul>
<pre id="log"></pre>
```
#### CSS
```css
#container,
#log {
height: 150px;
overflow: scroll;
}
#container li {
background-color: paleturquoise;
margin: 0.5rem;
}
```
#### JavaScript
```js
const add = document.querySelector("#add");
const remove = document.querySelector("#remove");
const reset = document.querySelector("#reset");
const container = document.querySelector("#container");
const log = document.querySelector("#log");
let namePrefix = 0;
add.addEventListener("click", () => {
const newItem = document.createElement("li");
newItem.textContent = `item ${namePrefix}`;
container.appendChild(newItem);
namePrefix++;
});
remove.addEventListener("click", () => {
const itemToRemove = document.querySelector("li");
if (itemToRemove) {
itemToRemove.parentNode.removeChild(itemToRemove);
}
});
reset.addEventListener("click", () => {
document.location.reload();
});
function logChanges(records, observer) {
for (const record of records) {
for (const addedNode of record.addedNodes) {
log.textContent = `Added: ${addedNode.textContent}\n${log.textContent}`;
}
for (const removedNode of record.removedNodes) {
log.textContent = `Removed: ${removedNode.textContent}\n${log.textContent}`;
}
if (record.target.childNodes.length === 0) {
log.textContent = `Disconnected\n${log.textContent}`;
observer.disconnect();
}
console.log(record.target.childNodes.length);
}
}
const observerOptions = {
childList: true,
subtree: true,
};
const observer = new MutationObserver(logChanges);
observer.observe(container, observerOptions);
```
#### Result
Try clicking "Add child" to add list items, and "Remove child" to remove them. The observer callback logs additions and removals. As soon as the list is empty, the observer logs a "Disconnected" message and disconnects the observer.
The "Reset example" button reloads the example so you can try it again.
{{EmbedLiveSample("Observing child elements", 0, 400)}}
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}