5

How could one trigger the default action/event of a HTML link (anchor element)? That is to use JavaScript/jQuery to "click" an existing HTML link, as if the user has clicked it.

Just using .click() does not seem to work.

$('#alink').click();
// the nothing happening

For this HTML:

<a id="alink" href="http://google.com" target="_blank">a link</a>

Example fiddle: http://jsfiddle.net/dCfD8/

I'd rather not create a new window in JavaScript (and take care of whatever else needs to be handled when a link is clicked).

Qtax
  • 33,241
  • 9
  • 83
  • 121
  • 1
    See this: http://stackoverflow.com/questions/1694595/can-i-call-jquery-click-to-follow-an-a-link-if-i-havent-bound-an-event-handl – Digital Plane Aug 25 '11 at 07:02
  • @Digital Plane, thanks, didn't see that one before. Altho it it would be interesting to know if there are any changes on the matter, or it the answer "it can't be done" still applies even for the latest browsers (eg Chrome 13+). – Qtax Aug 25 '11 at 07:13

4 Answers4

2

You can trigger the click event using a simple trigger method in jQuery.

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

Beware though, that even in the event gets fired, the browser will not follow the link href. The only way to follow the href is to actually click it with the mouse yourself.

As far as I know, there is no way to force a link to behave as if it were clicked. You have to change the document location or something like that to actually navigate between pages.

Jose Faeti
  • 12,126
  • 5
  • 38
  • 52
  • Accepted for "there is no way to force a link to behave as if it were clicked", which http://stackoverflow.com/questions/1694595/can-i-call-jquery-click-to-follow-an-a-link-if-i-havent-bound-an-event-handl also states. – Qtax Aug 26 '11 at 07:54
1

Expanding on Fabio Cicerchia's comment to his own post: You can use window.open:

var link = $('#alink');
var target = link.attr("target");
window.open(link.attr("href"), target ? target : "_self");
RoToRa
  • 37,635
  • 12
  • 69
  • 105
  • +1. Seems like that could work. But I'm worried that (default) popup handlers/blockers could block that, while they would affect a normal user clicking a link. – Qtax Aug 25 '11 at 19:40
  • Yes, actually most popup-blockers will block this, if it's not triggered by a user interaction. You coudl combine it with `doument.location=...` in the cases where the target is not set. Maybe you should explain the background, why you need to do this. Maybe that way alternatives could be found. – RoToRa Aug 26 '11 at 07:42
0
    <script src='jquery lib source' ></script>
    <script>
    function force()
    {    ...do something...to fill page2
         $('#gopage2').trigger('submit');
    }    
    </script>  
    <form action='#page2' id='gopage2'>
    </form>

    ...
    <span name='#page2'>This is page2</span>
  • 1
    Please consider adding some explanation to your answer, rather than just a code. Our goal here is to educate people so that they understand the answer and can apply it in other situations. If you comment your code and add an explanation, you will make your answer more helpful not just to the person who asked the question this time, but to anyone in the future who may be having the same problem. – starsplusplus Jun 18 '14 at 00:22
-1

try this:

$('#alink').trigger('click');
Fabio Cicerchia
  • 649
  • 3
  • 8
  • 23
  • 1
    this invoke only the jQuery events associated to click, if you want "emulate" the user click on an anchor to redirect to another url you can do this: `document.location = $('#alink').attr('href');` – Fabio Cicerchia Aug 25 '11 at 07:00
  • Would not work with `target="_blank"`, or any other special link attributes. – Qtax Aug 25 '11 at 07:06
  • 2
    The OP's `.click()` is just a shortcut version of `.trigger('click')`. – nnnnnn Aug 25 '11 at 07:27