Javascript - Auto Click on a Button on Page Load

Automatically click button on page load

First, this is not a good approach to make the customers/users to logging in. You can use cookie or sessions to auto login your user. Still, if you want to click the button auto on web page loading you can use the below script.

<script>
window.onload=function(){
$("#btnLoginMain").click();
}
</script>

use this in the header file of your html file. If you want to click the button after some time of page load use the setTimeout() function.

Auto click button on page load is not working

The autoclick is working, but it's just the fact that your anchor tag (a) is not leading the user anywhere. You probably were trying to do something like this:

<body>
<div><a id="redirect" ms-member-page="default" href="/mypage.html" class="w-button">Button Text</a></div>
<script src="https://d3e54v103j8qbb.cloudfront.net/js/jquery-3.4.1.min.220afd743d.js?site=5e79526aa1d3ba459f1ebac2" type="text/javascript" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="js/webflow.js" type="text/javascript"></script>
<!-- [if lte IE 9]><script src="https://cdnjs.cloudflare.com/ajax/libs/placeholders/3.0.2/placeholders.min.js"></script><![endif] -->
<script type="text/javascript">
document.getElementById("redirect").click();
</script>
</body>

Read more about anchor tags here.

a tags are for links, to send your user to a page. Buttons are made with the <button> tag.

Click JS button on page load

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>

Auto-click button element on page load using jQuery

You would simply use jQuery like so...

<script>
jQuery(function(){
jQuery('#modal').click();
});
</script>

Use the click function to auto-click the #modal button

auto click button when javascript function is called NOT on page load or button click

Since your logic is being ran as soon as it is reached by the parser, the elements that you are trying to manipulate must already exist for you to use them. So if the element appears on the page after the function call, it will not exist in the DOM yet for you to find and click it logically. Your script will need to be after the element in the DOM. Otherwise, you could put your logic in a document ready and then it would not execute until the entire page is parsed into the DOM.



Related Topics



Leave a reply



Submit