Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html> <head> <title>Title of the Document</title> </head> <body> <h1 id="elemId">Welcome to W3Docs</h1> <script> // catch on document document.addEventListener("hello", function(event) { alert("Hello from " + event.target.tagName); // Hello from H1 }); // dispatch on elem let event = new Event("hello", { bubbles: true }); elemId.dispatchEvent(event); // the document handler activates and displays a message </script> </body> </html>