Why does trigger event not trigger events defined with addEventListener

I created an example:

http://jsfiddle.net/y42pu5b6/

$(function(){
    function ping (){ 
        alert( "function fired" ); 
        console.log("fired"); 
    }
    console.log($("input#one")[0]);
    $("input#one")[0].addEventListener("change", ping);
    $("input#one").trigger("change");

    $("input#two").on("change", ping);
    $("input#two").trigger("change");

});

      

It looks like with jQuery you can use a "trigger" and an event to be applied when called addEventListener

. Is there a reason for this? I was under the notion that since the trigger is a jquery method, it only scans the jQuery object for handlers, not what is actually applied to the object.

I believe the trigger should fire for all applicable events, whether they were added via on

or addEventListener

. Maybe it takes a snapshot of the code on timeframe X and stores it in a jquery object. I'm not sure.

Maybe someone else has a deeper understanding of this than me?

+3


source to share


1 answer


Since different browsers handle different events differently, jQuery mitigates the problem by introducing its own Event abstraction layer (which also provides the event namespace among other benefits).

Therefore, when you call trigger

, jQuery triggers only its related events (using on

, bind

, click

etc.), but he does not try to run events connected with nature.



See http://learn.jquery.com/events/triggering-event-handlers/

+2


source







All Articles