Listen to jQuery event without jQuery

Is there a way to set up an event listener, so it captures the event fired by jQuery? I want to use just plain (vanilla) JavaScript to listen for the event.

I installed the following example:

(function($) {

    $(document).on('sent_jquery_rec_jquery', function () {
        console.log('sent_jquery_rec_jquery');
    });

    $(document).on('sent_vanilla_rec_jquery', function () {
        console.log('sent_vanilla_rec_jquery');
    });

    document.addEventListener('sent_vanilla_rec_vanilla', function () {
        console.log('sent_vanilla_rec_vanilla');
    });

    document.addEventListener('sent_jquery_rec_vanilla', function () {
        console.log('sent_jquery_rec_vanilla');
    });

    document.dispatchEvent(new Event("sent_vanilla_rec_vanilla"));
    document.dispatchEvent(new Event("sent_vanilla_rec_jquery"));

    $(document).trigger('sent_jquery_rec_jquery');
    $(document).trigger('sent_jquery_rec_vanilla');

}(jQuery));

Three events that are accepted and recorded:

  • sent_vanilla_rec_vanilla
  • sent_vanilla_rec_jquery
  • sent_jquery_rec_jquery

When an event is dispatched through $(document).trigger, the event listener configured with document.addEventListenerdoes not start. What is the reason for this and how can I make it work?

+4
source share
1 answer

JQuery , jQuery . IE < 9 , , jQuery 3 4 .

+1

Source: https://habr.com/ru/post/1606788/


All Articles