JavaScript - Print Specific Part of Page

Submitted by: 
Language: 
Visitors have accessed this post 238 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  9. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  10. </a> </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Print Specific Part of Page</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div style="border:1px solid #000; padding:10px;">
  17. <div id="div1">
  18. <h4>Morg</h4>
  19. <p>Morg was cross. She was more than cross, she was furious. She had been chosen to mind her little brother, again.</p>
  20. </div>
  21. <button onclick="print_page('div1')">Print</button>
  22. </div>
  23. <br />
  24. <div style="border:1px solid #000; padding:10px;">
  25. <div id="div2">
  26. <h4>End of the Line</h4>
  27. <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>
  28. </div>
  29. <button onclick="print_page('div2')">Print</button>
  30. </div>
  31. </div>
  32. <script src="js/script.js"></script>
  33. </body>
  34. </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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.