How to programmatically trigger the click on a link using jQuery?

Jquery

Jquery Problem Overview


How to programmatically trigger the click on a link using jQuery?

Jquery Solutions


Solution 1 - Jquery

$('#your_link_id').click()

See the excellent jquery docs for more information

Solution 2 - Jquery

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.

Solution 3 - Jquery

You can use trigger:

$('#your_link_id').trigger('click');

Solution 4 - Jquery

$('#your_link_id')[0].trigger('click');

is needed as jQuery returns an array and we cannot trigger click on multiple eventlinks. You have to target only a single element

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMohit JainView Question on Stackoverflow
Solution 1 - JqueryThomasView Answer on Stackoverflow
Solution 2 - JqueryJagView Answer on Stackoverflow
Solution 3 - Jquerync3bView Answer on Stackoverflow
Solution 4 - Jquerysandeep kaleView Answer on Stackoverflow