This question is the sequel of this one. However, it is not necessary to read the previous, I'm just giving a link for interested readers.
There is an observer, which will react on every element with some class, as @Shomz suggested:
var target = document.querySelectorAll(".someclass");
for (var i = 0; i < target.length; i++) {
create(target[i]);
}
function create(t) {
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var foo = t.getAttribute("aaa")
if (foo == "vvv")
t.style.backgroundColor = "red";
});
});
var config = {
attributes: true
};
observer.observe(t, config);
}
So, there are two closely intertwined questions.
1) For some reasons, the observer may be disconnected. How I can reconnect it? I tried to use observer.observe
, but it doesn't work here.
2) And the second question, what is the way to manually disconnect an observer? I tried to use observer.disconnect();
, but it is also doesn't work.
for
loop instead oftarget.forEach()
?target.forEach()