Do getElementsByClassName (and similar functions like getElementsByTagName and querySelectorAll) work the same as getElementById or do they return an array of elements?
The reason I ask is because I am trying to change the style of all elements using getElementsByClassName. See below.
//doesn't work
document.getElementsByClassName('myElement').style.size = '100px';
//works
document.getElementById('myIdElement').style.size = '100px';
Your
getElementByIdcode works since IDs have to be unique and thus the function always returns exactly one element (ornullif none was found).However, the methods
getElementsByClassName,getElementsByName,getElementsByTagName, andgetElementsByTagNameNSreturn an iterable collection of elements.The method names provide the hint:
getElementimplies singular, whereasgetElementsimplies plural.The method
querySelectoralso returns a single element, andquerySelectorAllreturns an iterable collection.The iterable collection can either be a
NodeListor anHTMLCollection.getElementsByNameandquerySelectorAllare both specified to return aNodeList; the othergetElementsBy*methods are specified to return anHTMLCollection, but please note that some browser versions implement this differently.Both of these collection types don’t offer the same properties that Elements, Nodes, or similar types offer; that’s why reading
styleoff ofdocument.getElements…(…)fails. In other words: aNodeListor anHTMLCollectiondoesn’t have astyle; only anElementhas astyle.These “array-like” collections are lists that contain zero or more elements, which you need to iterate over, in order to access them. While you can iterate over them similarly to an array, note that they are different from
Arrays.In modern browsers, you can convert these iterables to a proper Array with
Array.from; then you can useforEachand other Array methods, e.g. iteration methods:In old browsers that don’t support
Array.fromor the iteration methods, you can still useArray.prototype.slice.call. Then you can iterate over it like you would with a real array:You can also iterate over the
NodeListorHTMLCollectionitself, but be aware that in most circumstances, these collections are live (MDN docs, DOM spec), i.e. they are updated as the DOM changes. So if you insert or remove elements as you loop, make sure to not accidentally skip over some elements or create an infinite loop. MDN documentation should always note if a method returns a live collection or a static one.For example, a
NodeListoffers some iteration methods such asforEachin modern browsers:A simple
forloop can also be used:Aside:
.childNodesyields a liveNodeListand.childrenyields a liveHTMLCollection, so these two getters also need to be handled carefully.There are some libraries like jQuery which make DOM querying a bit shorter and create a layer of abstraction over “one element” and “a collection of elements”: