0.2.0 • Published 6 years ago
observe-el v0.2.0
observe-el
Easily react to changes in the DOM. Uses the MutationObserver API.
Install
$ npm install observe-el
Usage
// observer.js
const observeEl = require('observe-el');
// Assuming a DOM Element with className `_17wu2` exists
let observer = observeEl('._17wu2', () => console.log('💡 DOM Element changed!'));
// main.js
const p = document.querySelector('._17wu2');
p.innerText = 'Changed text';
// Output
...
//=> 💡 DOM Element changed!
API
observeEl(element, callback, options)
Returns an instance of MutationObserver
and registers the instance to receive notifications of DOM mutations on the specified node.
element
Type: string
| Node
If provided a string
, it expects it to be a valid CSS selector.
callback
Type: Function
The function which will be called on each DOM mutation. More on that here.
options
Type: object
Default: { attributes: true, characterData: true, childList: true }
Expects a MutationObserverInit
object with at least one of childList
, attributes
, or characterData
set to true
.
NOTE: All 3 options are set to true
by default. For a list of all valid options, see here.