1

The Event Object is different when I do:

$('section#top10 ul#periodes li').click(function (e) {

and when I do:

$('section#top10 ul#periodes li').trigger('click') {

I know the trigger function triggers the first function, but the "e" argument is not the same with the two methods. With the first method I can access the srcElement, but with trigger the object attributes are not the same. I would like to know why the two arguments are different when the same function is called.

5
  • Trigger Execute all handlers and behaviors attached to the matched elements for the given event type. Commented Oct 10, 2013 at 0:43
  • 1
    What object attributes exactly? Commented Oct 10, 2013 at 0:45
  • Well they are two objects completely different, so pretty much all attributes. Commented Oct 10, 2013 at 0:47
  • @Fals But that does not explain why the click handler has a different e argument. Commented Oct 10, 2013 at 0:48
  • @Maxwell The reason: Those are two different "click" event variants: One is a natural (user) click, and the other is a programmatic click (triggered from JavaScript alone). Commented Oct 10, 2013 at 0:49

1 Answer 1

1

Actually when a user clicks on an element, the event carries mouse position like pageX/pageY and clientX/clientY but using trigger to fire the event programmatically, there will be no real mouse positions because the click event fired without the mouse interaction, this is one big difference, at least I can think of. That's why the e/event object is different in both cases and it makes sense, IMO.

Update :

In other words, it's not a real click event but a simulation of that event and in this case it's a tailored peace, for example, this is the prototype of the initMouseEvent (with example and on MDN), which used with createEvent

object.initMouseEvent (eventName, bubbles, cancelable, view, detail, screenX, screenY, clientX, clientY, ctrlKey, altKey, shiftKey, metaKey, button, relatedTarget);

This used to simulate a mouse event in vanilla JavaScript (it may varies in different browsers), also look at this, so in case of jQuery it's (event object created by trigger) a tailored object that jQuery build for cross browser support when using trigger and it's not same as the one that is in real click event, completely two different objects.

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

Comments

Your Answer

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