0

I was wondering how I could get a button <button class="btn btn-primary btn-lg btn-withdraw">Text!</button> automatically clicked when a page is loaded. I've seen other posts about people attempting to do the same thing I am, but I do not understand what to do. Any help would be appreciated. Thanks, Aiden

Sources- Auto-click button element on page load using jQuery

Community
  • 1
  • 1
conleys
  • 11
  • 1
  • 2
  • If you _seen other posts about people attempting to do the same_ but did _not understand_, at least link those sources and highlight the points you need clarified. – Lulero Apr 09 '17 at 05:14
  • http://stackoverflow.com/questions/18646881/auto-click-button-element-on-page-load-using-jquery – conleys Apr 09 '17 at 05:15

3 Answers3

0

So, you'll have to put an id attribute to your button.

<button id="myButton" class="btn btn-primary btn-lg btn-withdraw">Text!</button>

Then, at the end of your html, you have to use Javascript inside a script tag to "click" on it as following.

<script>
document.getElementById('myButton').click()
</script>

It seems like you have to learn Javascript, search for tutorials on internet.

Sorikairo
  • 798
  • 4
  • 19
0

You can use the jQuery method .click()

Bernard R
  • 3
  • 3
0

Because you want the button to be clicked once the page is loaded, you want to make sure the DOM element itself has actually loaded before executing your code.

You can read more here: What is the DOM ready event?

The below code waits for the page to fully load before executing 'click' on the button.

Non-jQuery solution. It doesn't make sense to add jQuery to your page if you will only use it once.

window.onload = function () {
    document.querySelector('.btn.btn-primary.btn-lg.btn-withdraw').click();
}

Alternatively, you can add an ID to the button as suggested by Sorikairo and subsequently trigger the click:

//Add ID to button
<button id="myButton" class="btn btn-primary btn-lg btn-withdraw">Text!</button>

//Click on load.
window.onload = function () {
    document.getElementById('myButton').click()
}