I think this is best asked with code:
var event = new CustomEvent("custom", {bubbles: true}); // Create Event
// Create and attach element
var element = document.createElement("div");
document.body.appendChild(element);
element.addEventListener("custom", function(e) {
console.log("listening");
});
// At this point I thought that if the "custom" event were fired that my
// element listing would be triggered with the listener callback.
// But....
document.body.dispatchEvent(event);
// check the console... nothing :(
// the only way I am able to get my element event listener to fire is if
// the element calling the dispatchEvent(event) is the element itself.
element.dispatchEvent(event);
What am I doing wrong? Is this the way events are meant to work? My question is how do I listen to a custom event when the event is not dispatched
dispatchEvent(event)
from the element containing the listener callback
element.addEventListener("custom", function(){});