As a beginner in my journey of learning JavaScript
, I stumbled upon some enlightening information while reading through MDN
articles. I discovered that instead of using a traditional for loop, the forEach()
method can be utilized to add eventhandlers
to each button element.
let buttons = document.querySelectorAll("button");
buttons.forEach(function(button) {
button.onclick = function() {
alert("hello world")
};
});
I'm feeling a bit lost trying to comprehend what 'button' represents as a parameter, and how it functions when applied to all the buttons. Can someone offer me a clearer explanation on the use of 'button' here?