Skip to content

Instantly share code, notes, and snippets.

@k0pernikus
Created November 12, 2024 12:13
Show Gist options
  • Save k0pernikus/c03143806b319e8c3acc5215e4c106de to your computer and use it in GitHub Desktop.
Save k0pernikus/c03143806b319e8c3acc5215e4c106de to your computer and use it in GitHub Desktop.
custom event in do not bubble by default, one has to set bubbles true
const customEventHandling = () => {
document.addEventListener('this-is-my-custom-event', (event) => {
console.log('THIS SHOULD PRINT ON DISPATCHED EVENT!');
console.log(event)
});
document.onreadystatechange = () => {
if (document.readyState !== "complete") {
console.log('not ready yet');
return;
}
console.log('before dispatch');
const event = new Event('this-is-my-custom-event', {bubbles: true});
const element = document.querySelector('body');
console.log('event i want to dispatch', event);
console.log('element i dispatch the event on', element);
element.dispatchEvent(event);
console.log('after dispatch');
};
}
customEventHandling();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment