I have implemented the following code for observing changes in class:
$(document).ready(function () {
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.attributeName === "class") {
var attributeValue = $(mutation.target).prop(mutation.attributeName);
console.log("Class attribute changed to:", attributeValue);
}
});
});
observer.observe(document.querySelector(".xxx"), {
attributes: true
}); });
JSFiddle link: https://jsfiddle.net/8Lfj0ev3/
Ideally, I want this to listen for changes in all elements with class 'xxx'. But it is only listening to first element with xxx class and ignoring others. How can I fix this?