Saturday, October 25, 2025
HomeLanguagesJavascriptHow to add innerHTML to print page?

How to add innerHTML to print page?

Here is the task to add innerHTML to the print page. Here we are going to change the present content inside the <body> element and sets them with the new specified content using document.body with the help of .innerHTML which specify the content to be printed. We’re going to discuss a few methods. 

Example 1: In this example, the specified content that reside inside the div (id=”printThis”) get through innerHTML. And, body content is also set through innerHTML to the above specified content. 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>How to add innerHTML
      to print page?</title>
  </script>
</head>
 
<body>
    <center>
        <div id="printThis">
            <h1 style="color:green;">
        GeeksForGeeks
        </h1>
            <h3>How to add innerHTML to print page?
        </h3>
            <h4>
            neveropen<br>
            A Computer Science Portal for Geeks.
        </h4>
        </div>
        <input type="button"
               onclick="printArea('printThis');"
               Value="Print">
       
        <script type="text/javascript">
            function printArea(areaName) {
                var newcontent =
                    document.getElementById(areaName).innerHTML;
                var actContents = document.body.innerHTML;
                document.body.innerHTML = newcontent;
                window.print();
                document.body.innerHTML = actContents;
            }
        </script>
    </center>
</body>
 
</html>


Output: Before Click on Button:

  

After Click on Button:

  

Example 2: In this example, the specified content that reside inside the <body> originally get through innerHTML. And, body content is printed. 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>How to add innerHTML
      to print page?</title>
  </script>
</head>
 
<body>
    <center>
        <div id="printThis">
            <h1 style="color:green;">
        GeeksForGeeks
        </h1>
            <h3>How to add innerHTML to print page?
        </h3>
            <h4>
            neveropen<br>
            A Computer Science Portal for Geeks.
        </h4>
        </div>
        <input type="button"
               onclick="printArea('printThis');"
               Value="Print">
       
        <script type="text/javascript">
            function printArea(areaName) {
                var actContents = document.body.innerHTML;
                document.body.innerHTML = actContents;
                window.print();
            }
        </script>
    </center>
</body>
 
</html>


Output: Before Click on Button:

  

After Click on Button:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS