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?
source
share