Last active
March 31, 2023 17:38
-
-
Save simonewebdesign/4017724 to your computer and use it in GitHub Desktop.
onclick vs addEventListener - https://simonewebdesign.it/onclick-vs-addeventlistener/
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
<button id="btn">Click me!</button> | |
<script> | |
var btn = document.getElementById('btn'); | |
btn.onclick = function() { | |
// will be overwritten! | |
console.log('[onclick] foo'); | |
} | |
btn.onclick = function() { | |
console.log('[onclick] bar'); | |
} | |
btn.addEventListener('click', function() { | |
// won't work on IE < 9 | |
console.log('[EventListener] foo'); | |
}); | |
btn.addEventListener('click', function() { | |
// won't work on IE < 9 | |
console.log('[EventListener] bar'); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
super clear