Skip to content

Instantly share code, notes, and snippets.

@ziadoz
Created January 31, 2024 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziadoz/d0c16aa87edc863e5535d0951e9402c5 to your computer and use it in GitHub Desktop.
Save ziadoz/d0c16aa87edc863e5535d0951e9402c5 to your computer and use it in GitHub Desktop.
JS - Custom Events
https://tobiasahlin.com/blog/move-from-jquery-to-vanilla-javascript/
<a href="#">I am a link.</a>
<script>
document.addEventListener('DOMContentLoaded', () => {
const a = document.querySelector('a');
['load', 'click'].forEach((type) => {
a.addEventListener(type, (event) => {
event.preventDefault();
console.log(event.type);
});
});
a.dispatchEvent(new Event('load'));
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment