79765262

Date: 2025-09-15 14:21:57
Score: 1
Natty:
Report link

You want to call the addEventListener method on the element, for example:

let buttons = document.querySelectorAll("button");

buttons.forEach(myFunction);

function myFunction(button){
    button.addEventListener("click", function() {
        alert("I got clicked!");
    });
};

or, more simply:

let buttons = document.querySelectorAll("button");
for (let button of buttons) {
    button.addEventListener("click", function() {
        alert("I got clicked!");
    });
}

https://www.w3schools.com/jsref/met_element_addeventlistener.asp

Reasons:
  • Probably link only (1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: dieckie