Search code examples
javascriptmutation-observers

How to apply querySelectorAll() on NodeList[]?


for(var i = 0; i < mutation.addedNodes.length; i++) {

 var inputs = mutation.addedNodes[i];

 console.log(inputs);

}

I want to apply querySelectorAll() on inputs but it is throwing an error

Uncaught TypeError: mutation.addedNodes[i].querySelectorAll is not a function.


Solution

  • but it is throwing an error Uncaught TypeError: mutation.addedNodes[i].querySelectorAll is not a function.

    querySelectorAll is invoked on an Element (doesn't include TextNode), just check if mutation.addedNodes[i] is an Element or not.

    for(var i = 0; i < mutation.addedNodes.length; i++) 
    {
       var ele = mutation.addedNodes[i];
       if ( ele instanceof Element )
       {   
          ele.querySelectorAll( "*" ); //put your selector 
       }
    }