JavaScript Beginner Dom · 1 min read

Wait for DOMContentLoaded

Start the JavaScript learning path »

❮ JavaScript Home All Tutorials ❯

Why is getElementById null?

Run after DOM is ready.

Example

Interactive preview available
JavaScript Example
document.addEventListener('DOMContentLoaded', () => {
  const app = document.querySelector('#app');
  console.log(app);
});

Line-by-Line Walkthrough

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

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…