Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery to programmatically click an <a> link

I know this question has been asked before, but after a search on the web I can't seem to find a straight forward answer.

the HTML

<a id=myAnchor href=index.php> 

the jQuery (Both of these do not work)

 $('#myAnchor').click(); 

or

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

What is the simplest and most efficient way to achieve this?

like image 899
Stephen Croft Avatar asked Jan 31 '12 14:01

Stephen Croft


People also ask

How can add trigger click event in jQuery?

$( "#foo" ). trigger( "click" ); As of jQuery 1.3, . trigger() ed events bubble up the DOM tree; an event handler can stop the bubbling by returning false from the handler or calling the .

What is trigger method in jQuery?

jQuery trigger() Method The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.


1 Answers

Try this:

$('#myAnchor')[0].click(); 

It works for me.

like image 118
user3346133 Avatar answered Oct 04 '22 11:10

user3346133