Skip to content
Related Articles

Related Articles

HTML | DOM replaceChild() Method

View Discussion
Improve Article
Save Article
  • Last Updated : 26 Jul, 2019

The replaceChild() method in HTML DOM is used to replace a child node with a new node within the given parent node.


parentNode.replaceChild( newChild, oldChild )

Parameter Values: This method accept two parameters which are listed below:

  • newChild: It is the required parameter. It represents the new node which need to be inserted.
  • oldChild: It is the required parameter. It represents the node which is replaced by new node.

Return Value: It returns a node object which represents the replaced node.

Example: In this example the first <li> text is replaced with the new text.

<!DOCTYPE html>
        <title>DOM replaceChild() Method</title>
        <h1 style="color: green;">
            DOM replaceChild() Method
        <p>Sorting Algorithm</p>
        <ul id = "listitem"><li>Insertion sort</li>
            <li>Merge sort</li>
            <li>Bubble sort</li>
        <button onclick ="Geeks()">
            Click Here!
        function Geeks() {
            var doc = document.createTextNode("Quick sort");
            var list = document.getElementById("listitem").childNodes[0];
            list.replaceChild(doc, list.childNodes[0]);

Before click on the button:
After click on the button:

Supported Browsers: The browser supported by DOM replaceChild() method method are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!