Use addEventListener() to run code when an event occurs on an element.
const button = document.querySelector("button");
button.addEventListener("click", function() {
console.log("Button clicked!");
});
The first argument is the event type. The second is the handler function that runs when the event fires.