How to replace $(document).ready() in pure JavaScript without jQuery?
Hi everyone! I just started learning JavaScript and previously used jQuery. There I was used to writing code like this:
```javascript
$(document).ready(function() {
// my code that runs after DOM is loaded
console.log('Page is ready!');
});
```
Or the shorthand version:
```javascript
$(function() {
// code
});
```
Now I want to drop jQuery to reduce page size and write in pure JavaScript. But I don't know how to properly make an equivalent of `$(document).ready()` in native JS.
The problem is that if I just write a script in `` or at the beginning of ``, it may execute before the DOM tree is fully built. For example, I try to access an element that doesn't exist yet:
```javascript
document.querySelector('.my-class').
No answers yet.
Log in to leave an answer