Sunday, December 14, 2025
HomeLanguagesJavascriptHow to replace the entire HTML node using JavaScript ?

How to replace the entire HTML node using JavaScript ?

Given an HTML document and the job is to replace the entire HTML element by a new one with the help of JavaScript. A few approaches are discussed here.

Approach 1:

  • Take the new document as a form of string(eg.. Str = ‘< html > < /html > ‘).
  • Use .replace() method on HTML element and replace it with the new HTML Document(eg.. $(‘html’).html(Str)).

Example 1: This example implements the above approach.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        Replace the entire HTML node using JavaScript.
    </title>
    <script src=
    </script>
    <style>
        #div {
            background: green;
            height: 100px;
            width: 200px;
            margin: 0 auto;
            color: white;
        }
    </style>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP">
    </p>
    <button onclick="GFG_Fun();">
        click here
    </button>
    <p id="GFG_DOWN" style="color: green;">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        up.innerHTML = 
 "Click on the button to replace the entire HTML element.";
  
        function GFG_Fun() {
            var Str = 
'<!DOCTYPE HTML><html><head><title>Check if an element is a'+
' div in JavaScript.</title></head><body style = "text-align:center;">'+
'<h2 style = "color:green;">GeeksForGeeks</h2><p>This is replaced element.'+
                '</p></body>  </html>';
            $('html').html(Str);
        }
    </script>
</body>
  
</html>


Output:

  • Before clicking on the button:
  • After clicking on the button:

Approach 2:

  • Take the new document as a form of string(eg.. Str = ”).
  • Use .open() method on document and this method takes 2 parameters(first is “text/html” and second is “replace”). If we don’t use ‘Replace’ then method will call adds page history. So we would have to click back two times to go to the previous page. So, replace is necessary argument to pass.
  • To this new document use .write() method and pass the new document.
  • Use .close() method on document for it to work.

Example 2: This example implements the above approach.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
       Replace the entire HTML node using JavaScript.
    </title>
    <script src=
    </script>
    <style>
        #div {
            background: green;
            height: 100px;
            width: 200px;
            margin: 0 auto;
            color: white;
        }
    </style>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP">
    </p>
    <button onclick="GFG_Fun();">
        click here
    </button>
    <p id="GFG_DOWN" style="color: green;">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        up.innerHTML = "Click on the button to replace the entire HTML element.";
  
        function GFG_Fun() {
            var Str = 
                '<!DOCTYPE HTML><html><head><title>Check if an element is a div'+
                ' in JavaScript.</title></head><body style = "text-align:center;">'+
                '<h2 style = "color:green;">GeeksForGeeks</h2><p>'+
                'This is replaced element.</p></body>  </html>';
            var newHTML = document.open("text/html", "replace");
            newHTML.write(Str);
            newHTML.close();
        }
    </script>
</body>
  
</html>


Output:

  • Before clicking on the button:
  • After clicking on the button:
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32447 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6816 POSTS0 COMMENTS
Nicole Veronica
11953 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12031 POSTS0 COMMENTS
Shaida Kate Naidoo
6951 POSTS0 COMMENTS
Ted Musemwa
7202 POSTS0 COMMENTS
Thapelo Manthata
6898 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS