0

What I want to do is issue a click event, preferably with jQuery, on an anchor element, causing the browser to follow that link. I know that my anchor selectors are correct, as they are returning 1 from .length.

Here's what I've tried:

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

But it won't follow the link.

How am I doing it wrong?

Thanks!!

Alex
  • 64,178
  • 48
  • 151
  • 180

4 Answers4

2

Why not just parse the url and send the user to that link location?

var url = $('#awesomeLink').attr('href');
window.location.href = url;
Tejs
  • 40,736
  • 10
  • 68
  • 86
1

Try $('#awesomeLink').trigger("click");

Lance
  • 5,655
  • 4
  • 30
  • 32
  • Maybe I didn't read enough. I thought the question was how to trigger the even. Navigating to the link without triggering the event, however, would be Tejs's recipe spot on. – Lance May 17 '10 at 17:57
0

Instead of trying to click the link directly, just tell the browser to load a different page:

window.location = "my_link's_URL";

This question is similar to how-do-i-automatically-click-a-link-with-javascript.

Community
  • 1
  • 1
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
0

If you are just wanting to change the browser location try:

window.location.href = $('#awesomeLink').attr('href');

Otherwise if you are needing a bit more, then this question might help (as might the other questions about native clicking).

Community
  • 1
  • 1
Blair McMillan
  • 5,299
  • 2
  • 25
  • 45