can-dom-mutate
Version:
Dispatch and listen for DOM mutations
65 lines (49 loc) • 1.7 kB
Markdown
{{}} can-dom-mutate
can-dom-utilities
can-infrastructure
./package.json
Dispatch and listen for DOM mutations.
can-dom-mutate.static 0 methods
can-dom-mutate/modules 1 modules
`domMutate`
`can-dom-mutate` exports an object that lets you listen to changes
in the DOM using the [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver)
API.
```js
import {domMutate} from "can";
domMutate
// -> {
// onAttributeChange( documentElement, callback ),
// onConnected( documentElement, callback ),
// onDisconnected( documentElement, callback ),
// onNodeAttributeChange( node, callback ),
// onNodeConnected( node, callback ),
// onNodeDisconnected( node, callback )
// }
// listen to every attribute change within the document:
domMutate.onAttributeChange(document.documentElement, function(mutationRecord){
mutationRecord.target //-> <input>
mutationRecord.attributeName //-> "name"
mutationRecord.oldValue //-> "Ramiya"
})
```
If you want to support browsers that do not support the `MutationObserver` api, use
[can-dom-mutate/node] to update the DOM. Every module within CanJS should do this:
```js
var mutate = require('can-dom-mutate/node');
var el = document.createElement('div');
mutate.appendChild.call(document.body, el);
```
## Use
```js
import {domMutate, domMutateNode} from "can";
var element = document.createElement("div");
var teardown = domMutate.onNodeConnected(element, ()=>{
console.log("element inserted!");
});
setTimeout(function(){
domMutateNode.appendChild.call(document.body, element);
},1000);
```