Open In App

Replace a DOM Element with Another DOM Element in Place

Last Updated : 17 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

To replace a DOM element with another DOM element in JavaScript, you can use the replaceChild() method.

This allows to swap one element for another in the DOM, keeping the new element in the same position as the replaced one.

parentNode Property

This property returns the parent node of the defined node, as a Node object.

Syntax:

node.parentNode

Return value:

It returns a node object, which represents the parent node of a node, or null if there is no parent.

replaceChild() Method:

This method replaces a child node with a new node. This new node can be an existing node in the document, or can be newly created.

Syntax:

node.replaceChild(newNode, oldNode)

Return value:

It returns a node object, which represents the replaced node.

Example 1: This example creates a new <span> element and replace it with the <a> element using parentNode property and replace() method.

html
<!DOCTYPE html>
<html>

<head>
    <title>
        JavaScript | Replace dom element in place.
    </title>
</head>

<body style="text-align:center;" id="body">
    <p id="GFG_UP" style="font-size: 15px; font-weight: bold;">
    </p>
    <div>
        <a id="a" href="#">GeeksforGeeks</a>
    </div>
    <br>
    <button onclick="gfg_Run()">
        Click here
    </button>
    <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;">
    </p>
    <script>
        let el_up = document.getElementById("GFG_UP");
        let el_down = document.getElementById("GFG_DOWN");
        let today = 'Click button to replace <a> element in DOM';
        el_up.innerHTML = today;

        function gfg_Run() {
            let aEl = document.getElementById("a");
            let newEl = document.createElement("span");
            newEl.innerHTML = "replaced text!";
            aEl.parentNode.replaceChild(newEl, aEl);
            el_down.innerHTML =
                "<a> element is replaced by the <span> element.";
        }
    </script>
</body>

</html>

Output:

Replace-a-DOM-element-with-another-DOM-element-in-place

Example 2: This example creates a new <a> element and replace it with the previous <a> element using parentNode property and replace() method keeping the href property same.

html
<!DOCTYPE html>
<html>

<head>
    <title>
        JavaScript | Replace dom element in place.
    </title>
</head>

<body style="text-align:center;" id="body">
    <p id="GFG_UP" style="font-size: 15px; font-weight: bold;">
    </p>
    <div>
        <a id="a" href="https://www.geeksforgeeks.org">GeeksforGeeks</a>
    </div>
    <br>
    <button onclick="gfg_Run()">
        Click here
    </button>
    <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;">
    </p>
    <script>
        let el_up = document.getElementById("GFG_UP");
        let el_down = document.getElementById("GFG_DOWN");
        let today = 'Click button to replace <a> element in DOM';
        el_up.innerHTML = today;

        function gfg_Run() {
            let aEl = document.getElementById("a");
            let newEl = document.createElement("a");
            newEl.innerHTML = "New GFG link!";
            newEl.href = "https://www.geeksforgeeks.org";
            aEl.parentNode.replaceChild(newEl, aEl);
            el_down.innerHTML =
                "<a> element is replaced by the new <a> element.";
        }
    </script>
</body>

</html>

Output:

Replace-a-DOM-element-with-another-DOM-element-in-place

Supported browsers:

  • Google Chrome
  • Mozilla Firefox
  • Safari
  • Opera

Next Article

Similar Reads