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!
This question is related to
jquery
html
button
modal-dialog
pageload
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();
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);
});
_x000D_
.container{
padding-top:50px;
}
_x000D_
<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>
_x000D_
We should rather use Javascript.
<button href="images/car.jpg" id="myButton">
Here is the Button to be clicked
</button>
<script>
$(document).ready(function(){
document.getElementById("myButton").click();
});
</script>
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>
Use the following code
$("#modal").trigger('click');
Source: Stackoverflow.com