Computer >> Computer tutorials >  >> Programming >> Javascript

How to use JavaScript to replace the content of a document with JavaScript?


You can try to run the following code to learn how to use JavaScript to replace the content of a document in JavaScript −

Example

<!DOCTYPE html>
<html>
   <body>
      <p>Click and replace this content.</p>
      <button onclick = "Display()">Replace</button>
      
      <script>
         function Display() {
            document.open("text/html","replace");
            document.write("<p>Demo text!</p>");
            document.close();
         }
      </script>
      
   </body>
</html>