Javascript DOM HTML document adoptNode() Method

Introduction

Adopt the first <h1> element that appears in an iframe:

View in separate window

<!DOCTYPE html>
<html>
<body>

<iframe src="https://www.java2s.com" style="height:380px;width:520px;"></iframe>
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {// w  w  w .  j a  v  a2 s .  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>

The adoptNode() method adopts a node from another document.

The adopted node can be of all node types.

All child nodes and its descendants the adopted node are also adopted.

The original node and its child nodes are removed from the other document.

Use document.importNode() method to copy a node, without removing it, from another document.

Use element.cloneNode() method to copy a node, without removing it, from the current document.

document.adoptNode(node)

Parameter Values

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

P:The adoptNode() method returns a Node object representing the adopted node




PreviousNext

Related