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 wich 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.
<!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> GeeksforGeeks<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> originaly get through innerHTML. And, body content is printed.
<!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> GeeksforGeeks<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:

Recommended Posts:
- How to replace innerHTML of a div using jQuery?
- HTML | DOM innerHTML Property
- Difference between innerText and innerHTML
- How to display search result of another page on same page using ajax in JSP?
- How to redirect a page to another page in HTML ?
- How to show Page Loading div until the page has finished loading?
- CSS | @page rule
- Refresh a page using PHP
- ES6 | Page Printing
- ES6 | Page Redirect
- Page Object Model (POM)
- Download web page using Java
- How to get the title of an HTML page ?
- How to refresh a page using jQuery?
- CSS | page-break-after Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



