We may want to add a new item to an HTML document. The first step is to create the node (element) that you want to add, the next step is to find the location in the document where you want to add it, and the last step is to do so. The syntax used to create a node is very simple, you only call a method (method) of the document object.
- createElement(): Creates a node of the specified name.
- createTextNode: allows you to add text to the node you specify.
- appendChild(): allows you to set where you want to node.
- insertBefore(): Used to add / move an existing item.
- removeChild(): removes a specified child node of the specified item.
- replaceChild():replaces a child node of the specified node with another node.
- cloneNode(): creates a copy of a node, and returns the copy.
- adoptNode(): This method is used to accept a node in another document.
- hasChildNodes(): Returns true if the specified node has a child node. Returns a false node if the specified node does not have a child node.
- importNode(): This method imports another node from a document.
HTML DOM adoptNode() Method
- The AdoptNode () method is used to accept the node in another document. The accepted node can be on all node types.
- If you have a grandchild of the knot, this is adopted.
- The original node is removed from the other document.
Syntax:
document.adoptNode(node)
- node: Any type of node is required.
adoptNode Examples
Example 1
<iframe src="/default.asp" style="height:380px;width:520px;"></iframe>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var frame = document.getElementsByTagName("IFRAME")[0]
var h = frame.contentWindow.document.getElementsByTagName("H1")[0];
var x = document.adoptNode(h);
document.body.appendChild(x);
}
</script>