7

I have this simple code : here

$(".btn").on('click',function () {

  $(".a").trigger('click');
});

$(".btn2").on('click',function () {

  $(".a")[0].click();
});

I'm trying to simulate pressing on Anchor.

But when I use jQuery's trigger, it doesn't work (why?)

When I use "jsobj".click() func, it does work.

After reading the jQuery documentation, I don't see any reason why it shouldn't.

Help ?

PS: I'm using Chrome.

4
  • Did you try $(".a")[0].trigger('click') ? Commented Jul 14, 2012 at 19:14
  • 1
    this will work , the question is why trigger('click') doesnt work Commented Jul 14, 2012 at 19:14
  • 2
    Very interesting observation indeed.. Commented Jul 14, 2012 at 19:37
  • You'll find a clearer answer here: stackoverflow.com/questions/13505003/… Commented Jan 9, 2017 at 19:40

3 Answers 3

7

Actually $(".a").trigger('click'); triggers the click event but it doesn't mean that it'll click the link, instead it'll execute the event handler if you already have one, i.e.

$(".btn, .btn2").on('click',function () {
    $($(".a")[0]).trigger('click'); // first element
});

$(".a").on('click', function (e){
    alert(e.target);
});​

The given example will trigger the click event of the a and will execute the handler (the anonymous function) that's already have been registered for that event using

$(".a").on('click', function (e){...});

DEMO.

Sign up to request clarification or add additional context in comments.

4 Comments

so whtqa is the difference between click and trigger click ?
you should have a handler defined to trigger a click event then both way you can trigger the event, i.e. $(".a").trigger('click'); or $($(".a")[0]).click(); but you have to have a click handler for that.
sorry, i dont understand why trigger click on button will actually do as if i pressed it , while trigger href wont
this is different scenario.here you attached explictly the on click to the href.
3

because $(".a")[0] return raw JavaScript node you cannot use jQuery object methods for that.

1 Comment

but if i bind to it a click event , and then use trigger, it will work... so...?
2

This is because JQuery's .trigger() doesn't really trigger the native events. Try this for example in your script:

$(".btn").on('click',function () {

  $(".a").trigger('click');
});
$(".a").click(function(){alert('triggered!')});

When you create a costume handler using JQuery, THEN the event will be triggered with the .trigger() method.

Update: This is quite interesting, seems to happen only with <a> tags AND with href. Check this out

2 Comments

where it says that trigger doesnt trigger the native events ( altoght i know its the answer by behavior )
@RoyiNamir, Check the example in the update, very interesting. As for where it says, I've just concluded that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.