+ 2
How do select multiple IDs in JS?
JS selection of IDs
6 odpowiedzi
+ 2
ENIOLA OPEYEMI
You can use getElementsByTagName("button"); which will return nodes of each button tag and using this.id you can get id of each button.
+ 2
🅰🅹 🅐🅝🅐🅝🅣 THANKS SO MUCH.
+ 1
I want to select multiple IDs.
+ 1
HTML
<button id= "one">1<button>
<button id= "two">2<button>
<button id= "three">3<button>
<button id= "four">4<button>
<button id= "five">5<button>
<button id= "six">6<button>
<button id= "seven">7<button>
<button id= "eight">8<button>
<button id= "nine">9<button>
<button id= "ten">10<button>
JS
document.getElementById("one"). disabled= true;
document.getElementById("two"). disabled= true;
document.getElementById("three"). disabled= true;
document.getElementById("four"). disabled= true;
document.getElementById("five"). disabled= true;
document.getElementById("six"). disabled= true;
document.getElementById("seven"). disabled= true;
document.getElementById("eight"). disabled= true;
document.getElementById("nine"). disabled= true;
document.getElementById("ten"). disabled= true;
So if I want to select more IDs I will not have to write this much code.
So I need some methods to shortening the code.
Thanks in advance 😊.
+ 1
Please write a code for that because I have tried using document.getElementsByTagName("button") but it's only working for the first button on the page.
Help me.
+ 1
ENIOLA OPEYEMI
Do this
window.onload = function() {
var ele = document.getElementsByTagName("button");
for (var i = 0; i < ele.length; i++) {
var id = ele[i].id;
console.log(id);
}
}