showing results for - "how to write a mutation observer js"
Luca
22 Apr 2018
1const observer = new MutationObserver(list => {
2    console.log('mutation list', list);
3});
4observer.observe(document.body, {
5    attributes: true,
6    childList: true,
7    subtree: true
8});
9// perform any DOM change action in your page. e.g. show/hide/remove
Joaquín
14 Apr 2017
1let mList = document.getElementById('myList'),
2            options = {
3                childList: true,
4                attributes: true,
5                subtree: true
6            },
7            observer = new MutationObserver(mCallback);
8
9        function mCallback(mutations) {
10            for (let mutation of mutations) {
11                // If you remove a child from the container you are watching
12                if (mutation.type === 'childList') {
13                    console.log('Mutation Detected: A child node has been added or removed.');
14                }
15                // If you use setAttribute to add a class or ID to an element
16                if (mutation.type === 'attributes') {
17                    console.log('Mutation Detected: An attribute has been added or removed.');
18                }
19
20                if (mutation.type === 'subtree') {
21                    console.log('Mutation Detected: A subtree has been added or removed.');
22                }
23            }
24        }
25
26observer.observe(mList, options);