Answer the question
In order to leave comments, you need to log in
How to select all buttons on a page?
While studying JS, I came to the querySelectorAll and querySelector search methods.
On an empty html page I created 2 buttons:
<body>
<button>Show</button>
<button>Show1</button>
</body>
let bShow = document.querySelectorAll('button');
bShow.onclick = function () {
// body...
alert('Hi');
};
querySelector('button')
Answer the question
In order to leave comments, you need to log in
you have to cycle through the buttons to make it work on each
let bShow = document.querySelectorAll('button');
for (let i = 0; i < bShow.length; i++) {
bShow[i].onclick = function() {
alert('Hi');
}
}
For example
document.querySelectorAll('button[class^="Show"]');
let btn = document.getElementsByTagName('button');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question