Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vdegenne/mutationobserver
easify the MutationObserver use
https://github.com/vdegenne/mutationobserver
Last synced: about 2 months ago
JSON representation
easify the MutationObserver use
- Host: GitHub
- URL: https://github.com/vdegenne/mutationobserver
- Owner: vdegenne
- Created: 2019-10-13T22:31:22.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2019-10-15T02:45:35.000Z (about 5 years ago)
- Last Synced: 2024-11-17T10:18:20.474Z (about 2 months ago)
- Language: HTML
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# MutationObserver
Wrapper that makes MutationObserver more easy to use.
## usage
Include the script in the page
```html
```
then on one of your element
```javascript
const elementToWatch = document.getElementById('myElement')
const observer = MutationObserver.observe(elementToWatch, (mutations, element) => {
console.log(`myElement changed`)
})
```### disconnecting
```javascript
observer.disconnect()
```### subtree
By default the observer only watches the children of the element.
To watch the subtree pass `true` to the 3rd argument :```javascript
MutationObserver.observe(
elementToWatch,
(mut, el) => { /* callback */ },
true
)
```### child_list & character_data
By default the observer only responds when nodes are added or removed.
If you want to watch for textual changes only provide the type as the 2nd argument :```javascript
/* character data */
MutationObserver.observe(elementToWatch, MutationObserver.CHARACTER_DATA, callback)
// same as
MutationObserver.observe(elementToWatch, 'character_data', callback)
// same as
MutationObserver.observe(elementToWatch, 2, callback)/* child list (default) */
MutationObserver.observe(elementToWatch, MutationObserver.CHILD_LIST, callback)
// same as
MutationObserver.observe(elementToWatch, 'child_list', callback)
// same as
MutationObserver.observe(elementToWatch, 1, callback)
// same as
MutationObserver.observe(elementToWatch, callback) // default
```## Installation
```npm i @vdegenne/mutation-observer```