I wanna be able to observe the body element for changes in its class attribute.
I found this code online:
var elemToObserve = document.getElementById('your_elem_id');
var prevClassState = elemToObserve.classList.contains('your_class');
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if(mutation.attributeName == "class"){
var currentClassState = mutation.target.classList.contains('your_class');
if(prevClassState !== currentClassState) {
prevClassState = currentClassState;
if(currentClassState)
console.log("class added!");
else
console.log("class removed!");
}
}
});
});
observer.observe(elemToObserve, {attributes: true});
And I didn't really understand what's going on with the prevClassState. Thanks in advance.