This answer is wrong, please see the comments below.
Document.getElementById()
and methods or wrappers that rely on it return a reference.
Likewise for Document.querySelector()
.
This all stems from the DOM4 spec, which states that (emphasis mine):
A collection
is an object that represents a lists of DOM nodes. A collection
can be either live
or static
. Unless otherwise stated, a collection must be live
.
From a programming point of view this notion is easy to understand, you waste less memory and no time is spent copying things around, leading to faster execution.
However, for Document.querySelectorAll()
we have an interesting case:
The NodeList
object returned by the querySelectorAll()
method must be static
.
This means, as MDN puts it, that Document.querySelectorAll()
Returns a non-live NodeList
of all the matching element nodes.
This means that the returned set is not a set of references to the original nodes, but instead to a set of nodes that represent the nodes at the time the query was executed. This means that a copy of all the matching nodes had to be made and you're no longer working with references to the live nodes.