Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu September 04, 2022 . #class . #Element

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 click a button dynamically in Javascript after loading the page.

Syntex :

document.querySelector('button').click();

Trigger click event on div by class name.

  1. 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>
  2. 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 »
save
listen
AI Answer
Write Your Answer
loading
back
View All