JavaScript - adoptNode() method

revision:


Category : document

Document.adoptNode() transfers a node from another document into the method's document. The adopted node and its subtree are removed from their original document (if any), and their ownerDocument is changed to the current document. The node can then be inserted into the current document.

Syntax :

        document.adoptNode(externalNode)
    

Parameters:

externalNode : required. The node from another document to be adopted. Can be of any node type.

Examples:

            const iframe = document.querySelector("iframe");
            const iframeImages = iframe.contentDocument.querySelectorAll("img");
            const newParent = document.getElementById("images");

            iframeImages.forEach((imgEl) => {
                newParent.appendChild(document.adoptNode(imgEl));
            });
        

Practical examples

example:
code:
                    
                

example:
code:
                    
                

example:
code: