Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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

like image 442
Mohit Jain Avatar asked May 17 '10 07:05

Mohit Jain


People also ask

How do you trigger a click event for a hyperlink?

Answer: Use the jQuery click() Method You can use the click() method to trigger a click on a link programmatically using jQuery.

How can add trigger click event in 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.

How do you trigger a click element?

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.


2 Answers

$('#your_link_id').click() 

See the excellent jquery docs for more information

like image 164
Thomas Avatar answered Oct 02 '22 15:10

Thomas


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.

like image 45
Jag Avatar answered Oct 02 '22 17:10

Jag