Tijmen
Tijmen

Reputation: 13

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?

Upvotes: 1

Views: 2056

Answers (3)

offset82
offset82

Reputation: 19

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

Upvotes: 2

Sarout
Sarout

Reputation: 902

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

Upvotes: 2

fkrzski
fkrzski

Reputation: 255

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>

Upvotes: 2

Related Questions