return elements that match specified CSS selector(s)

Does querySelectorAll() return live NodeList?

Answers:

  • No, the querySelectorAll() method does not return a live NodeList. Instead, it returns a static NodeList, meaning that the NodeList reflects the state of the DOM at the time the method is called. If the DOM changes after the NodeList has been created (for example, if elements are added or removed that match the selector), the NodeList will not be updated to reflect those changes.

    For a live collection of elements, you would typically use methods such as getElementsByTagName() or getElementsByClassName(), which do return live NodeLists that automatically update when the DOM changes. However, it's worth noting that most modern development prefers using querySelector() and querySelectorAll() for their flexibility and CSS selector support, accepting that they return static NodeLists.