Auto-click button element on page load using jQuery

254,022

Solution 1

You would simply use jQuery like so...

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

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

Solution 2

JavaScript Pure:

<script type="text/javascript">
document.getElementById("modal").click();
</script>

JQuery:

<script type="text/javascript">
$(document).ready(function(){
    $("#modal").trigger('click'); 
});
</script>

or

<script type="text/javascript">
$(document).ready(function(){
    $("#modal").click(); 
});
</script>

Solution 3

Use the following code

$("#modal").trigger('click');

Solution 4

I tried the following ways in first jQuery, then JavaScript:

jQuery:

 window.location.href = $(".contact").attr('href');
 $('.contactformone').trigger('click');  

This is the best way in JavaScript:

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

Solution 5

You can use that and adjust the time you want to launch 1= onload 2000= 2 sec

$(document).ready(function(){
 $('#click').click(function(){
        alert('button clicked');
    });
  // set time out 2 sec
     setTimeout(function(){
        $('#click').trigger('click');
    }, 2000);
});
.container{
padding-top:50px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="col text-center">
<button id="click" class="btn btn-danger">Jquery Auto Click</button>
  </div>
   </div>

Share:
254,022

Related videos on Youtube

JStormThaKid
Author by

JStormThaKid

Coder/Web Dev

Updated on February 09, 2021

Comments

  • JStormThaKid
    JStormThaKid about 3 years

    If I wanted to auto-click a button element on page load, how would I go about this using jQuery?

    The button html is

    <button class="md-trigger" id="modal" data-modal="modal"></button>
    

    Any help on this topic would be greatly appreciated! Thanks in advance!

  • Ananta Prasad
    Ananta Prasad over 8 years
    repetition of answers..!!
  • Reverend Bubbles
    Reverend Bubbles over 4 years
    Will this trigger an ng-click on that button as well?
  • Dasun Bandara
    Dasun Bandara over 3 years
    click() is deprecated now. Use " trigger("click") " instead. <script> jQuery(function(){ jQuery('#modal').trigger('click'); }); </script>
  • rinilnath
    rinilnath almost 2 years
    How to make it trigger only once? .this one loads it every time when page is ready