JavaScript
Beginner
Dom
· 1 min read
Wait for DOMContentLoaded
Why is getElementById null?
Run after DOM is ready.
Example
Interactive preview availabledocument.addEventListener('DOMContentLoaded', () => {
const app = document.querySelector('#app');
console.log(app);
});
Line-by-Line Walkthrough
Or place <script defer> / put scripts before </body>.