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>
Related videos on Youtube
Comments
-
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 over 8 yearsrepetition of answers..!!
-
Reverend Bubbles over 4 yearsWill this trigger an
ng-click
on that button as well? -
Dasun Bandara over 3 yearsclick() is deprecated now. Use " trigger("click") " instead. <script> jQuery(function(){ jQuery('#modal').trigger('click'); }); </script>
-
rinilnath almost 2 yearsHow to make it trigger only once? .this one loads it every time when page is ready