addEventListener

  • Why not myButton.addEventListener("click", handleClick());?
  • Because to call addEventListener, JavaScript needs to call handleClick first
  • That ends up essentially "clicking" the button
  • myButton.addEventListener("click", handleClick); does not
  • It tells addEventListener what function to call when the button is clicked
  • ... but it does not run the function right away
  • Known as a callback function

6 / 21