I have been interested about this topic for quite some time. These methods are as follows:
getElementsByTagNamegetElementsByClassNamegetElementsByNamequerySelectorAll
As far as I know, these DOM methods are the only methods that can return frozen or live NodeLists . For some of these methods, the order is determined by the W3C specification. For example, http://www.w3.org records the following for NodeLists returned by querySelectorAll
The querySelectorAll () methods in the document, DocumentFragment, and Element Interfaces should return a NodeList containing all matching element nodes in the node context subtrees in the document order. If there are no matching nodes, the method should return an empty NodeList.
However, I could not find similar clear specifications for the other methods that I mentioned. My questions are here:
- Is there a specific order (most likely document order) for the results?
- how reliable and cross-browser are these specifications?
Absolutely clear:
<div>this</div> <div>is</div> <div>a demo</div> // is this always guaranteed to be "<div>is</div>" document.querySelectorAll('div')[1]
Andre Meinhold
source share