Add observeMutations.md
This commit is contained in:
23
snippets/observeMutations.md
Normal file
23
snippets/observeMutations.md
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
### observeMutations
|
||||||
|
|
||||||
|
Runs the provided callback for each mutation on the specified element.
|
||||||
|
|
||||||
|
Use a [`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) to observe mutations on the given element.
|
||||||
|
Use `Array.forEach()` to run the callback for each mutation that is observed.
|
||||||
|
Omit the third argument, `options`, to use the default [options](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver#MutationObserverInit) (all `true`).
|
||||||
|
|
||||||
|
```js
|
||||||
|
const observeMutations = (element, callback, options) => new MutationObserver(mutations => mutations.forEach(m => callback(m))).observe(element, Object.assign(
|
||||||
|
{
|
||||||
|
childList: true,
|
||||||
|
attributes: true,
|
||||||
|
attributeOldValue: true,
|
||||||
|
characterData: true,
|
||||||
|
characterDataOldValue: true,
|
||||||
|
subtree: true
|
||||||
|
}, options));
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
observeMutations(document, console.log); // Logs all mutations that happen on the page
|
||||||
|
```
|
||||||
@ -119,6 +119,7 @@ negate:function
|
|||||||
nthElement:array
|
nthElement:array
|
||||||
objectFromPairs:object,array
|
objectFromPairs:object,array
|
||||||
objectToPairs:object,array
|
objectToPairs:object,array
|
||||||
|
observeMutations:browser,event,advanced
|
||||||
off:browser,event
|
off:browser,event
|
||||||
on:browser,event
|
on:browser,event
|
||||||
once:function
|
once:function
|
||||||
|
|||||||
Reference in New Issue
Block a user