Categories
JavaScript Answers

How to Make a JavaScript Function Wait Until an Element Exists Before Running it?

Spread the love

Using the MutationObserver to Watch for Element to be Added to the DOM

We can use the MutationObserver constructor available with modern browsers to watch for an element appearing.

For instance, if we have the following HTML:

<div id='container'>

</div>

Then we can append a child element to it and watch it appear by writing:

const container = document.getElementById('container')
setTimeout(() => {
  const div = document.createElement('div')
  div.textContent = 'hello'
  div.id = 'hello'
  container.appendChild(div)
}, 2000)

const observer = new MutationObserver((mutations, obs) => {
  const hello = document.getElementById('hello');
  if (hello) {
    console.log(hello.innerText)
    obs.disconnect();
    return;
  }
});

observer.observe(document, {
  childList: true,
  subtree: true
});

We call setTimeout with a callback that appends a div as the child of the div with ID container .

Then we invoke the MutationObserver constructor with a callback that has the mutation and obs parameters.

obs is the returned observer.

In the callback, we try to get the element with ID hello .

If it exists, then we get the innerText property value of the element.

And then we call disconnect stop watching for changes in the DOM.

Then we call observer.observe to watch for changes in the document .

childList is set to true to watch for adding or removing of the elements.

subtree is also set to true to watch for child element changes.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

5 replies on “How to Make a JavaScript Function Wait Until an Element Exists Before Running it?”

The mutation observer code is run first so that it could observer all the changes in the DOM.

return is still needed if the expression you’re returning doesn’t start on the same line as the signature.

But it’s optional otherwise.

Why is this so complicated? This seems not as a native solution, but as a clutch. There should be a simple one command approach, like:

document.waitElementById(‘container’)…

Leave a Reply

Your email address will not be published. Required fields are marked *