addEventListener

  • The element.addEventListener method lets you define event listeners in JS
  • You can use it to keep HTML clean...
  • Or when you want to respond to an event in multiple ways
<h1>My Page</h1>
<button id="my-button">Click Me</button>
const myButton = document.getElementById("my-button");
myButton.addEventListener("click", handleClick);

function handleClick() {
    myButton.innerText = "Thanks"
}

2 / 21