Sometimes, we want to trigger clicks automatically with jQuery.
In this article, we’ll look at how to trigger clicks automatically with jQuery.
How to trigger clicks automatically with jQuery?
To trigger clicks automatically with jQuery. we can call the click
method.
For instance, we write:
<button>
hello
</button>
to add a button.
Then we write:
const button = $('button')
button.on('click', () => console.log('clicked'))
$(document).ready(() => {
$(button).click();
});
We select the button with $('button')
.
Then we call on
with 'click'
and a click event handler to add an event handler.
Then we call click
on the button
when the document is loaded.
Therefore, we see 'clicked'
logged when the page loads.
Conclusion
To trigger clicks automatically with jQuery. we can call the click
method.