Wait for Element to exist via Mutation Observer

const waitForEl = (selector) => { return new Promise((resolve, reject) => { let el = document.querySelector(selector); if (el) {resolve(el);} new MutationObserver((mutationRecords, observer) => { Array.from(document.querySelectorAll(selector)).forEach((element) => { resolve(element); observer.disconnect(); }); }) .observe(document.documentElement, { childList: true, subtree: true }); }); } waitForEl("#yo").then(function(element) { alert('yooo'); });

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.