Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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```