Skip to content

Instantly share code, notes, and snippets.

@avtarnanrey
Created October 10, 2019 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avtarnanrey/d3fbad56a2a073e357bf5ac747f68bef to your computer and use it in GitHub Desktop.
Save avtarnanrey/d3fbad56a2a073e357bf5ac747f68bef to your computer and use it in GitHub Desktop.
Wait for element to be added into DOM then execute JavaScript
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (!mutation.addedNodes) return
for (var i = 0; i < mutation.addedNodes.length; i++) {
var item = Array.prototype.slice.call(mutation.addedNodes).find(function (i) { return i.id == "selector" });
if (item) {
// Do your stuff here
observer.disconnect();
}
}
})
})
observer.observe(document.querySelector("#parent_tag"), {
childList: true,
subtree: true,
attributes: false,
characterData: false
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment