How to programmatically trigger the click on a link using jQuery?
Answer: Use the jQuery click() Method You can use the click() method to trigger a click on a link programmatically using jQuery.
When an event handler is added using . on( "click", function() {...} ) , it can be triggered using jQuery's . trigger( "click" ) because jQuery stores a reference to that handler when it is originally added. Additionally, it will trigger the JavaScript inside the onclick attribute.
The HTMLElement. click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.
$('#your_link_id').click()
See the excellent jquery docs for more information
If you have an anchor link:
<a id="my_link_id" href="something">My Link</a>
it will fail as other answers have mentioned. Calling .eq and .trigger('click') doesn't work for me, but this does:
$('#your_link_id').get(0).click();
In my specific case, I've assigned a blob url programmatically to the anchor's href.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With