How do select multiple IDs in JS? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How do select multiple IDs in JS?

JS selection of IDs

8th May 2021, 2:25 PM
ENIOLA OPEYEMI
ENIOLA OPEYEMI - avatar
6 Answers
+ 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.
8th May 2021, 3:27 PM
A͢J
A͢J - avatar
19th May 2021, 8:44 AM
ENIOLA OPEYEMI
ENIOLA OPEYEMI - avatar
+ 1
I want to select multiple IDs.
8th May 2021, 3:09 PM
ENIOLA OPEYEMI
ENIOLA OPEYEMI - avatar
+ 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 😊.
8th May 2021, 3:25 PM
ENIOLA OPEYEMI
ENIOLA OPEYEMI - avatar
+ 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.
8th May 2021, 3:31 PM
ENIOLA OPEYEMI
ENIOLA OPEYEMI - avatar
+ 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); } }
8th May 2021, 3:32 PM
A͢J
A͢J - avatar