← CodePrimer
JavaScript Beginner Dom

Wait for DOMContentLoaded

Why is getElementById null?

Run after DOM is ready.

Sample
document.addEventListener('DOMContentLoaded', () => {
  const app = document.querySelector('#app');
  console.log(app);
});

Notes

Or place <script defer> / put scripts before </body>.

Keywords: DOMContentLoaded · ready · null

Related

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share