EN
JavaScript - getElementById with multiple IDs
1
answers
0
points
How can I get element handle by multiple IDs?
I want something like:
document.getElementById("myId1" "myId2" "myId3");
1 answer
0
points
You can use querySelectorAll() method. It allows to specify multiple ids in a CSS selector string.
Note:
getElementById()only supports one id at the time and returns a single node, not an array of nodes.
Practical example
// ONLINE-RUNNER:browser;
<html>
<body>
<div id="id1">element1</div>
<div id="id2">element2</div>
<div id="id3">element3</div>
<script>
var handles = document.querySelectorAll("#id1, #id2, #id3");
for(var i = 0; i < handles.length; ++i) {
console.log(handles[i].outerHTML); // do something with the elements (e.g display their outerHTML)
}
</script>
</body>
</html>
See also
References
0 comments
Add comment