Document adoptNode() Method - Javascript DOM

Javascript examples for DOM:Document adoptNode

Description

The adoptNode() method adopts a node from another document and removes the original node and its child nodes from the other document.

Parameter Values

Parameter TypeDescription
node? Node object Required. The node from another document. Can be of any node type

Return Value:

A Node object, representing the adopted node

The following code shows how to Adopt the first <h1> element that appears in an iframe

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<iframe src="http://java2s.com" style="height:380px;width:520px;"></iframe>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/*from   ww  w  .j a  va2s . c  o m*/
    var frame = document.getElementsByTagName("IFRAME")[0]
    var h = frame.contentWindow.document.getElementsByTagName("H1")[0];
    var x = document.adoptNode(h);
    document.body.appendChild(x);
}
</script>

</body>
</html>

Related Tutorials