简体   繁体   中英

Click JS button on page load

I want my webpage to auto click a button when the page is loaded. This is my button:

 <button class="configure-product configure-product-simple elementor-button" type="button" data-price="95" data-product_id="1934">Stel samen!</button>

I think this button calls a JS function of a wordpress plugin, so that you open the product configurator. If i copy this button html on another page tho it wont work. It will only work on the product page.

Im very new to developing, so i could really use some help. Does anyone have any ideas how to implement this?

this is how to do it in jQuery

$(document).ready(function(){ //on document loads (you could change to window also)
  $('#click').click(); // click
})

 $(document).ready(function(){ $('#click').click(); }) $('#click').click(()=>{ console.log('thanks;'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="click"> Click Meeh </button>

another shorter way to do it in vanilla js

 document.onload = document.querySelector('#click').click();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="click" onclick="console.log('hi')"> Click Meeh </button>

other ways to do it

  1. https://stackoverflow.com/a/38517365/18001301
  2. window.onload vs document.onload

document.getElementById("input").click();

 window.onload = function() { document.getElementById("my-button").click(); }
 <button id="my-button" class="configure-product configure-product-simple elementor-button" type="button" data-price="95" data-product_id="1934">Stel samen!</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM