Search code examples
javascriptdomappendchildmutation-observers

Number of nodes returned in MutationRecord.addedNodes nodelist (mutationObserver)


MutationRecord.addedNodes returns NodeList with nodes detected as added in my document. When I use obj.appendChild() method, mutationObserver detects it and returns this ONE node in MutationRecord.addedNodes as nodelist [node]. When I use .appendChild() in loop, mutationObserver fires as many times as the number of loops amounts.

I'm just wondering if there is any case when mutationOvserver will detect more than one node added simultaneously and fire ONCE and return those nodes in MutationRecord.addedNodes as ONE nodelist [node,node,node,...]?


Solution

  • Judging by the spec one appendChild corresponds to a single-node addedNodes array.

    addedNodes usually contains many nodes during the initial page load and, naturally, if many nodes were inserted by js code in one operation via insertAdjacentHTML, or document.write, or .innerHTML, or .outerHTML, or DocumentFragment with many nodes.