+ 1

How to select all buttons with forEach in JS dom (no jQuery)

I have 3 buttons having class name "btn"; How can select all button by using addEventListener and forEach? I don't select one by one HTML codes: <div class="display"> <input type="number" class="text"> </div> <div class="buttons"> <input type="button" value="1" class="btn1"> <input type="button" value="2" class="btn"> <input type="button" value="3" class="btn"> </div> JS code: const display = document.querySelector(".text"), btn = document.querySelectorAll(".btn"); btn.addEventListener("click", buttons); function buttons() { display.value = btn.value; };

6th Jul 2018, 5:23 PM
Abdul Basit
Abdul Basit - avatar
1 Antwort