I want to detect if a button has been pressed on a document. That means I don’t want to put in document.getElementById("ID").onclick
for each button. In other words, i want to detect if ANY button has been pressed on the document. If a button is pressed, get that button object, get it’s textContent and that’s all there is to it.
Advertisement
Answer
Have you tried this?
let buttons = document.querySelectorAll(".button"); buttons.forEach(btn=>{ btn.addEventListener("click",function(e){ btn.innerText = "CLICKED BUTTON" }); });
<button class="button">1</button> <button class="button">2</button> <button class="button">3</button> <button class="button">4</button>