I currently have the code:
$('.example').bind('DOMNodeInserted DOMNodeRemoved', function(event) {
....
});
Which works fine, but it's not very effective and has been deprecated since then. What is the best way to do this?
I searched for MutationObserver, but does this code work?
It gives the error "is mutation.addedNodesnot a function." I will also need removedNodes.
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node.className == 'example') {
....
}
});
});
});
observer.observe(document, {
childList: true,
subtree: true,
attributes: false,
characterData: false,
});
source
share