What does this do?
button.addEventListener("click", () => {
console.log("clicked");
});Options
- It clicks the button immediately on page load
- It registers a function that runs on every click (Correct option)
- It deletes the button from the page when clicked
- It logs a message once, when the page loads
Answer
Registers a function that runs every time the button is clicked
Why
addEventListener(eventName, callback) tells the browser: 'when this event happens on this element, call this function'.
The function is a callback: you give it to the browser, and the browser calls it later, once per click.
Other common events: "input", "submit", "keydown", "scroll".
Remember
addEventListener = run this function when that happens.