Categories
JavaScript Answers

How to listen for DOM ready event with JavaScript?

Spread the love

Sometimes, we want to listen for DOM ready event with JavaScript.

In this article, we’ll look at how to listen for DOM ready event with JavaScript.

How to listen for DOM ready event with JavaScript?

To listen for DOM ready event with JavaScript, we listen to the DOMContentLoaded event.

For instance, we write

const onReady = () => {
  /* ... */
};

document.addEventListener("DOMContentLoaded", onReady);

to call addEventListener to listen for the DOMContentLoaded event.

We set onReady as its event listener.

It’s called when the DOM is done loading.

Conclusion

To listen for DOM ready event with JavaScript, we listen to the DOMContentLoaded event.

By John Au-Yeung

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

Leave a Reply

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