A NodeList can be either live or static depending on how it is created. Most NodeLists, like those returned by document.querySelectorAll()
are static. This means they do not update automatically when the DOM changes. However, NodeLists created by properties like childNodes
are live.
In the above code, the static NodeList is created using document.querySelectorAll('.item')
, and the live NodeList is created using container.childNodes
.
Initially, the static NodeList contains 2 elements with the class item
, so the length of static value is 2. The live NodeList reflects all child nodes (including text nodes), so its length is 6.
When the "Add Item" button is clicked, a new <div>
with the class item
is dynamically added to the DOM.
The static NodeList does not update, so the length of static value remains 2 but the live NodeList automatically updates to include the newly added element, reflecting the changes in the DOM without needing to re-query it and logs 6.