Skip to main content

JavaScript - Print Specific Part of Page


In this tutorial we will create a Print Specific Part of Page using JavaScript. This code can print a specific parts of the document when the user select a print button in an area. The code use onclick() to initiate the print_page() function in order to print a specific page when you add div id as a parameter of it. This is a free program, you can modify it and use it as your own.

We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <head>
  3. <link type="text/css" rel="stylesheet" href="css/bootstrap.css">
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. </head>
  6. <nav class="navbar navbar-default">
  7. <div class="container-fluid">
  8. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  9. </div>
  10. </nav>
  11. <div class="col-md-3"></div>
  12. <div class="col-md-6 well">
  13. <h3 class="text-primary">JavaScript - Print Specific Part of Page</h3>
  14. <hr style="border-top:1px dotted #ccc;"/>
  15. <div style="border:1px solid #000; padding:10px;">
  16. <div id="div1">
  17. <h4>Morg</h4>
  18. <p>Morg was cross. She was more than cross, she was furious. She had been chosen to mind her little brother, again.</p>
  19. </div>
  20. <button onclick="print_page('div1')">Print</button>
  21. </div>
  22. <br />
  23. <div style="border:1px solid #000; padding:10px;">
  24. <div id="div2">
  25. <h4>End of the Line</h4>
  26. <p>When Frank and I stepped through the post office doors, there was a crowd gathered, gawking at the new fixture on the wall like a chorus of wide-mouthed frogs.</p>
  27. </div>
  28. <button onclick="print_page('div2')">Print</button>
  29. </div>
  30. </div>
  31. <script src="js/script.js"></script>
  32. </body>
  33. </html>

Creating the Script

This code contains the script of the application. This code will print a specific parts of the page when the button is clickedTo do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. function print_page(div){
  2. var page = document.body.innerHTML;
  3. var el = document.getElementById(div).innerHTML;
  4.  
  5. document.body.innerHTML = el;
  6. window.print();
  7. document.body.innerHTML = page;
  8. }

There you have it we successfully created a Print Specific Part of Page using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.