addEventListener

  • Sometimes it's helpful to add event listeners in JavaScript
  • This keeps the HTML a bit cleaner
  • Less awkward for creating listeners for elements that were created in JS
  • Provide the event to watch for and the function to call
  • Don't put "on" in the event name
const myButton = document.getElementById("some-button");
myButton.addEventListener("click", doSomething);
function doSomething() {
    console.log("clicked")
}

15 / 17