Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
MutationObserver: Listen Indefinitely On Changing Element
// support multiple browsers
// http://stackoverflow.com/questions/2844565/is-there-a-jquery-dom-change-listener/11546242#11546242
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var elementId = 'some-id';
// instantiate initial observer
var observer = new MutationObserver(observeIndefinitely(elementId));
observer.observe(document.getElementById(elementId), {
// expensive, only use what's needed of these
attributes: true,
childList: true,
subtree: true,
characterData: true
});
function observeIndefinitely (id) {
return function (mutations, observer) {
// do stuff...
observer.disconnect();
// re-select the element and set up a subsequent observer
var nextObserver = new MutationObserver(observeIndefinitely(id));
nextObserver.observe(document.getElementById(id), {
// expensive, only use what's needed of these
attributes: true,
childList: true,
subtree: true,
characterData: true
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment