mutation observer that works
Change class name on click in jQuery
by ian_smithz
HTML
<div id="sticky-right-content">
xxx
</div>
<script>
var times = 10;
function changeDiv() {
$('#sticky-right-content').append('<br/>Here is more content!');
if (times > 0) {
setTimeout(function(){
times--;
changeDiv();
}, 3000);
}
}
changeDiv();
</script>
JavaScript
// Select the node that will be observed for mutations
const targetNode = document.getElementById('sticky-right-content');
// Options for the observer (which mutations to observe)
const config = { attributes: true, childList: true, subtree: true };
// Callback function to execute when mutations are observed
const listerCallback = function(mutationsList, observer) {
// Use traditional 'for loops' for IE 11
for(let mutation of mutationsList) {
if (mutation.type === 'childList') {
console.log('A child node has been added or removed.');
}
else if (mutation.type === 'attributes') {
console.log('The ' + mutation.attributeName + ' attribute was modified.');
}
}
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(listerCallback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
// Later, you can stop observing
//observer.disconnect();