ReplaceChild (): Method replaces one child node within a given parent element with another child node:
Referencre = Element.replacechild (newchild,oldchild);
The Oldchild node must be an element child node whose return value is a reference pointer to the child node that has been replaced.
In the following example, the element with the id attribute value is content and an element with an id attribute value is fineprint, we will create a new text element with the createelement () method, and then use the ReplaceChild () method to replace the FinePrint element with the newly created element:
var container = document.getElementById ("content"); var message = document.getElementById ("FinePrint"); var para = document.createelement ("P"); Container.replacechild (para,message);
If the inserted element itself has child nodes, those child nodes are also inserted before the target node.
The ReplaceChild () method can also replace another existing node with an existing node on the document tree. If the newchild () node is an existing node on the document tree, the ReplaceChild () method will first delete the word and use it to replace the Oldchild () node.
In the following example, the ReplaceChild () method uses the id attribute value to be the headline element to replace the element that contains the id attribute value of the content element: FinePrint.
var container = document.getElement.ById ("content"); var message = document.getElementById ("FinePrint"); var announcement = document.getElementById ("headline"); Container.replacechild (announcement,message);
Replace node (ReplaceChild ())