Search code examples
javascriptdomcollectionsnodelist

How to distinguish between live and non-live NodeList collections?


Both document.getElementsByTagName('div') and document.querySelectorAll('div') return NodeList collection. The only difference is that first method returns live-collection and second one - a static one.

The question is - is there any opportunity to distinguish one object from another only via inspecting these objects (i.e - not trying to add/remove some items to test "liveness")?


Solution

  • The NodeList interface is agnostic of its dead or live status.

    interface NodeList {
      Node item(in unsigned long index);
      readonly attribute unsigned long length;
    };
    

    It only contains a property length, and a method item so I'm afraid it's currently not possible to determine if an object is live without manipulating the DOM and seeing the effects.