Calling a function when page loads is as easy as to cut a cake.
Trigger click event on div by class name.
- Create a div element with a class and also add the onclick attribute, so that whenever the div element is clicked, we can alert something.
<div class="my-div" onclick="alert('You clicked me')"> Click me </div>
- Now, select the div element by the class name and add the click() method to click the div element dynamically.
document.querySelector('.my-div').click();Try it Yourself »
Onclick function being called when page loads
Calling a function when page loads is as easy as to cut a cake. Use the click() method to …