0

I have a anchor on which I trigger a click with JavaScript. Please see this fiddle.

$(function() {
    $('a').click();
});

For some reason, triggering the click does not bring me to the page specified by the href attribute of the anchor.

How can I have the click event also bring me to the page linked by the anchor?

Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111
Randomblue
  • 105,985
  • 138
  • 338
  • 537

2 Answers2

1

It will not work like that, the call $('a').click(); is a short cut for $('a').trigger('click'); which tries to simulate a click action on the anchor element.

But the simulation is not absolute, though it will fire the registered event handler it will not completely simulate a use click trigger the default action.

As per the jquery doc for trigger

Description: Execute all handlers and behaviors attached to the matched elements for the given event type.

and

Although .trigger() simulates an event activation, complete with a synthesized event object, it does not perfectly replicate a naturally-occurring event.

So I don't think it is possible to completely simulate a user action and trigger a redirection using this method

Arun P Johny
  • 376,738
  • 64
  • 519
  • 520
0

Seems to be something to do with jQuery and the trigger method, works fine when you change [object object] to [object]. Here is the fiddle. Enjoy!

$(function() {
    $('a').get(0).click();
});