Doing some research regarding live or static DOM collection. What I got was that a NodeList is a live collection when changes in the DOM are reflected in the collection. It is a static collection when this is not the case.
Was reading the following question:
The answer stated that HTMLCollection
s are always live and NodeList
may be either live or static.
However is this still the case? I can't seem to reproduce a static collection. For example document.querySelectorAll
is supposed (source: answer from the link earlier given) to return a static collection but seems live to me in the following example:
const elements = document.querySelectorAll('.hi');
function change() {
document.querySelectorAll('.hi')[0].style.backgroundColor = 'red';
}
function checkvalue() {
console.log(elements[0].style.backgroundColor);
}
<div class="hi">test</div>
<div class="hi">test</div>
<button onclick="change()">change</button>
<button onclick="checkvalue()">checkvalue</button>
In this example the elements NodeList
still updates the color red and thus the elements NodeList
seems to be live.
Question:
Are there still live and static DOM collections, or do only live ones exist nowadays? If static ones still exist please give an example.