addeventlistener javascript to all buttons code example
Example: how to add event listener to all buttons
<button class="btn">
Button1
</button>
<button class="btn">
Button2
</button>
<button class="btn">
Button3
</button>
<button class="btn">
Button4
</button> //HTML
//JavaScript
var buttons = document.querySelectorAll(".btn").length;
for (var i = 0; i < buttons ; i++) {
document.querySelectorAll(".btn")[i].addEventListener("click", function() {
alert("Button Clicked");
});
}