Get multiple elements by Id
If you can change the markup, you might want to use class
instead.
HTML
<a class="test" name="Name 1"></a>
<a class="test" name="Name 2"></a>
<a class="test" name="Name 3"></a>
JS
var elements = document.getElementsByClassName("test");
var names = '';
for(var i = 0; i < elements.length; i++) {
names += elements[i].name;
}
document.write(names);
jsfiddle demo
GetElementByID - Multiple IDs
document.getElementById()
only supports one name at a time and only returns a single node not an array of nodes. You have several different options:
- You could implement your own function that takes multiple ids and returns multiple elements.
- You could use
document.querySelectorAll()
that allows you to specify multiple ids in a CSS selector string . - You could put a common class names on all those nodes and use
document.getElementsByClassName()
with a single class name.
Examples of each option:
doStuff(document.querySelectorAll("#myCircle1, #myCircle2, #myCircle3, #myCircle4"));
or:
// put a common class on each object
doStuff(document.getElementsByClassName("circles"));
or:
function getElementsById(ids) {
var idList = ids.split(" ");
var results = [], item;
for (var i = 0; i < idList.length; i++) {
item = document.getElementById(idList[i]);
if (item) {
results.push(item);
}
}
return(results);
}
doStuff(getElementsById("myCircle1 myCircle2 myCircle3 myCircle4"));
find multiple elements by id
Use contains or starts-with?
pricesToFind = driver.find_elements_by_xpath(".//*[contains(@id,'dcm-reservation-limit-multiple-input-generic-')]")
pricesToFind = driver.find_elements_by_xpath(".//*[starts-with(@id,'dcm-reservation-limit-multiple-input-generic-')]")
select multiple elements by ID in one line
Give all your required elements a class and select them through getElementsByClassName(..)
.
(and maybe use jQuery to do the same thing with much less pain)
How can I pass multiple ids in getElementById
You can use querySelectorAll
and then attach click events to all divs with id matching active_*
. In the click event, you can use the split
method to extract the number part and then use to access your dom elements.
let elements = document.querySelectorAll("[id^='active_']");
for (const element of elements) { element.addEventListener('click', function(event) { let index = this.id.split('_')[1]; console.log(`gender_${index}`, `size_${index}`, `q_${index}`); });}
div { margin-bottom: 1em;}
<div id='active_0'>a</div><div id='active_1'>b</div><div id='active_2'>c</div><div id='active_3'>d</div>
Related Topics
Jquery: Get Selected Element Tag Name
How to Show a Hidden Div When a Select Option Is Selected
Html5 Canvas Drawimage Ratio Bug Ios
Best Way to Obfuscate an E-Mail Address on a Website
Detecting the System Dpi/Ppi from Js/Css
Is There Any Cross-Browser JavaScript For Making Vh and Vw Units Work
How to Access and Process Nested Objects, Arrays, or Json
How to Include a JavaScript File in Another JavaScript File
How to Print a Number With Commas as Thousands Separators in JavaScript
Positioning ≪Div≫ Element At Center of Screen
How to Get a Style Attribute from a CSS Class by JavaScript/Jquery
Submit Form Fields Inside Display:None Element
Jquery - How to Determine If a Div Changes Its Height or Any CSS Attribute