3

Could someone explain to me what exactly this means

trigger executes the code for the event handler but not actually executes the event itself?

Consider the following code:

$('#parent').on('click',function (event){
   console.log(event);
});

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

trigger does produce an Event object since it consoles it. So, in which way it does not execute the event?

2
  • 2
    So, in which way it does not execute the event? events are not executed, event handlers are. Commented Jan 22, 2016 at 9:49
  • 1
    @Satpal No, on button, the native DOM method click or click event bound using relevant js obstuvise/unobstrusive method are called jsfiddle.net/ftxvf106 or more relevant jsfiddle.net/vt38n1zo Commented Jan 22, 2016 at 9:57

1 Answer 1

4

jQuery has a rather nice event system, that handles events in a more complex way than the basic event bubbling. For instance it allows multiple events, of the same type, on an element.

That means that jQuery events sit on top of the browser events and add features. When you trigger a jQuery event it just send messages up the jQuery DOM tree, which causes things to respond, but it does not necessarily generate the native browser events.

You can still hit the native event generation by using the underlying DOM elements and methods that jQuery wraps:

// This will generate a click event "for jQuery to hear"
$('#parent').trigger('click');

or this does exactly the same as above:

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

Whereas this will fire the native DOM click event "actually clicks the button/link":

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

Update (from A. Wolff):

Some jQuery click events are passed on to the native click event. An exception being anchor links (which do not). I would have to test it to see if native clicks are generated or not for buttons and other inputs.

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

7 Comments

It should be noted that triggering click event using jQuery will call the native DOM API method, only anchor tags are explecitely excluded github.com/jquery/jquery/blob/master/src/event.js#LC515
@A.Wolff: I have certainly seen that on anchors, which makes sense. Will update for that specific case.
@A.Wolff i just want to ask in my $('#select').change(function() { }); where select is a select with option. i have a $('.input').trigger('change'); where input is an input field. and inside $('.input').change(function() { }); i have alert. Why doesnt my alert appear?
@A.Wolff i think you just solve it how would i check the input if i added it dynamically?i add the input dynamically but my event are handled like this $(document).on('change keyup', '.input', function(e) { so i thought it will be called.
@A.Wolff pls dont remove the last link to the fiddle thank you
|

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.