I’m trying to get the “quantity” attribute of multiple Elements ID at the same time.
Here’s what I tried :
productMinimum : document.getElementsById("id1","id2").getAttribute("quantity")
How can I make this work? The “id1” and “id2” elements all have the “quantity” attribute.
Here’s what it looks like from the HTML side :
<li class="product" name="RD-101" id="id1" price="18" quantity="2">
<li class="product" name="RD-101" id="id2" price="18" quantity="4">
The problem you’re having is that
getElementsById()doesn’t exist (unless you’ve defined it elsewhere). What you should be using isgetElementById(), albeit twice (asgetElementById(), as its name implies, returns only one element, even if there are multiple elements with that sameid, which is invalid mark-up, so please don’t do that), and then pushing the returned elements into an array:You could, of course, create your own function to return multiple elements based on their
id:JS Fiddle demo.
Bear in mind, though, that this returns a regular array, not a
nodeList(as would be returned, for example, bygetElementsByClassName()). And the returned array, even if it’s only a single element, would have to be iterated over in the same way as any other array.References:
getElementById().nodeList.push().