Created
December 5, 2024 16:07
-
-
Save prof3ssorSt3v3/287b7459326ad06b7c7d9fd87e6d2975 to your computer and use it in GitHub Desktop.
Event discussion file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Events</title> | |
<style> | |
body { | |
font-family: sans-serif; | |
padding: 0; | |
margin: 0; | |
} | |
header { | |
h1 { | |
padding: 20px; | |
background-color: steelblue; | |
color: aliceblue; | |
margin: 0; | |
} | |
} | |
</style> | |
<script> | |
document.addEventListener('DOMContentLoaded', init); | |
function init() { | |
//create the event objects | |
let ev = new Event('poke'); | |
let mouseEv = new MouseEvent('click', { clientX: 20, clientY: 20 }); | |
let customEv = new CustomEvent('sparkle', { detail: { special: true, num: Math.random() } }); | |
let h1 = document.querySelector('header h1'); | |
//add listeners | |
h1.addEventListener('poke', (ev) => { | |
console.log(ev); | |
}); | |
h1.addEventListener('click', (ev) => { | |
console.log(ev); | |
}); | |
h1.addEventListener('sparkle', (ev) => { | |
console.log(ev); | |
}); | |
//trigger the events | |
h1.dispatchEvent(ev); | |
h1.dispatchEvent(mouseEv); | |
h1.dispatchEvent(customEv); | |
} | |
</script> | |
</head> | |
<body> | |
<header> | |
<h1>Events</h1> | |
</header> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment