Skip to main content

Printing a Specific HTML Element using jQuery Tutorial

Screenshot
Body

In this tutorial, we will tackle about how to create a javascript function that Prints a Specific HTML Element using jQuery. This can help you to lessen your work as a programmer in writing and the lines of your codes. This technique can also prevent code duplication.

The Print function that we will be creating is the cloned data from the HTML. Our goal for this tutorial is to duplicate the HTML Element using jQuery along the elements that contains the codes for styling the element to preserve the design of the element.

Getting Started

Download the jQuery library. For the user interface design, I will be using Bootstrap v5.

Creating the Interface

The script below is the HTML file for our index page of the web application. This contains the elements the element we will print later on. Save this file as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Print Element using jQUery</title>
  9. <link rel="stylesheet" href="./css/bootstrap.min.css">
  10. <script src="./js/jquery-3.6.0.min.js"></script>
  11. <script src="./js/bootstrap.min.js"></script>
  12. <script src="./js/script.js"></script>
  13. <!-- Custom CSS -->
  14.  
  15. </style>
  16. </head>
  17.  
  18. <body class="bg-light">
  19. <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient" id="topNavBar">
  20. <div class="container">
  21. <a class="navbar-brand" href="https://Sourcecodester.com">
  22. Sourcecodester
  23. </a>
  24. </div>
  25. </nav>
  26. <div class="container py-3" id="page-container">
  27. <h3><b>How to Print Data of Specific Element using jQUery</b></h3>
  28. <hr>
  29. <div id="printables">
  30. <div class="to_print" id="first_element">
  31. <h3 class="text-center"><b>First Element Sample Data</b></h3>
  32. <hr>
  33. <button class="btn btn-sm btn-success float-end rounded-0 my-2" class="button" onclick="print_element('first_element')">Print First Element</button>
  34. <table class="table table-bordered table-striped">
  35. <tr class="bg-primary bg-gradient text-white">
  36. <th class="text-capitalize text-center py-1 px-2">name</th>
  37. <th class="text-capitalize text-center py-1 px-2">phone</th>
  38. <th class="text-capitalize text-center py-1 px-2">email</th>
  39. <th class="text-capitalize text-center py-1 px-2">address</th>
  40. <th class="text-capitalize text-center py-1 px-2">region</th>
  41. <th class="text-capitalize text-center py-1 px-2">text</th>
  42. </tr>
  43. </thead>
  44. <tr>
  45. <td class="px-2 py-1">Caldwell Stewart</td>
  46. <td class="px-2 py-1">(723) 398-8511</td>
  47. <td class="px-2 py-1">[email protected]</td>
  48. <td class="px-2 py-1">Ap #499-3016 Tincidunt St.</td>
  49. <td class="px-2 py-1">Antwerpen</td>
  50. <td class="px-2 py-1">Mauris vestibulum, neque sed dictum eleifend, nunc risus varius orci,</td>
  51. </tr>
  52. <tr>
  53. <td class="px-2 py-1">Jesse Serrano</td>
  54. <td class="px-2 py-1">(426) 346-7475</td>
  55. <td class="px-2 py-1">[email protected]</td>
  56. <td class="px-2 py-1">Ap #197-2368 Netus Rd.</td>
  57. <td class="px-2 py-1">Nordrhein-Westphalen</td>
  58. <td class="px-2 py-1">enim consequat purus. Maecenas libero est, congue a, aliquet vel,</td>
  59. </tr>
  60. <tr>
  61. <td class="px-2 py-1">Shea Donovan</td>
  62. <td class="px-2 py-1">1-512-856-0720</td>
  63. <td class="px-2 py-1">[email protected]</td>
  64. <td class="px-2 py-1">429-7540 Nulla. Avenue</td>
  65. <td class="px-2 py-1">British Columbia</td>
  66. <td class="px-2 py-1">lectus rutrum urna, nec luctus felis purus ac tellus. Suspendisse</td>
  67. </tr>
  68. <tr>
  69. <td class="px-2 py-1">Joshua Malone</td>
  70. <td class="px-2 py-1">(784) 452-6346</td>
  71. <td class="px-2 py-1">[email protected]</td>
  72. <td class="px-2 py-1">P.O. Box 461, 3954 Arcu. Street</td>
  73. <td class="px-2 py-1">Michoac√°n</td>
  74. <td class="px-2 py-1">laoreet ipsum. Curabitur consequat, lectus sit amet luctus vulputate, nisi</td>
  75. </tr>
  76. <tr>
  77. <td class="px-2 py-1">Basia Haynes</td>
  78. <td class="px-2 py-1">1-770-885-2056</td>
  79. <td class="px-2 py-1">[email protected]</td>
  80. <td class="px-2 py-1">Ap #187-2876 Phasellus Av.</td>
  81. <td class="px-2 py-1">Henegouwen</td>
  82. <td class="px-2 py-1">sagittis semper. Nam tempor diam dictum sapien. Aenean massa. Integer</td>
  83. </tr>
  84. </tbody>
  85. </table>
  86. </div>
  87.  
  88. <div class="to_print" id="second_element">
  89. <h3 class="text-center"><b>Second Element Sample Data</b></h3>
  90. <hr>
  91. <button class="btn btn-sm btn-success float-end rounded-0 my-2" class="button" onclick="print_element('second_element')">Print Second Element</button>
  92. <table class="table table-bordered table-striped">
  93. <tr class="bg-gradient bg-dark text-white">
  94. <th class="text-capitalize text-center py-1 px-2">name</th>
  95. <th class="text-capitalize text-center py-1 px-2">phone</th>
  96. <th class="text-capitalize text-center py-1 px-2">currency</th>
  97. <th class="text-capitalize text-center py-1 px-2">numberrange</th>
  98. <th class="text-capitalize text-center py-1 px-2">text</th>
  99. </tr>
  100. </thead>
  101. <tr>
  102. <td class="py-1 px-2">Laurel O'neill</td>
  103. <td class="py-1 px-2">(516) 671-6981</td>
  104. <td class="py-1 px-2">$2.60</td>
  105. <td class="py-1 px-2">1</td>
  106. <td class="py-1 px-2">massa. Quisque porttitor eros nec tellus. Nunc lectus pede, ultrices</td>
  107. </tr>
  108. <tr>
  109. <td class="py-1 px-2">Ulric Murray</td>
  110. <td class="py-1 px-2">1-607-328-3542</td>
  111. <td class="py-1 px-2">$92.00</td>
  112. <td class="py-1 px-2">6</td>
  113. <td class="py-1 px-2">at pede. Cras vulputate velit eu sem. Pellentesque ut ipsum</td>
  114. </tr>
  115. <tr>
  116. <td class="py-1 px-2">Diana Santiago</td>
  117. <td class="py-1 px-2">(712) 367-4128</td>
  118. <td class="py-1 px-2">$17.18</td>
  119. <td class="py-1 px-2">7</td>
  120. <td class="py-1 px-2">semper, dui lectus rutrum urna, nec luctus felis purus ac</td>
  121. </tr>
  122. <tr>
  123. <td class="py-1 px-2">Ramona Wood</td>
  124. <td class="py-1 px-2">(640) 623-7192</td>
  125. <td class="py-1 px-2">$44.30</td>
  126. <td class="py-1 px-2">7</td>
  127. <td class="py-1 px-2">Nulla tincidunt, neque vitae semper egestas, urna justo faucibus lectus,</td>
  128. </tr>
  129. <tr>
  130. <td class="py-1 px-2">Michelle Mueller</td>
  131. <td class="py-1 px-2">1-328-121-0647</td>
  132. <td class="py-1 px-2">$44.25</td>
  133. <td class="py-1 px-2">6</td>
  134. <td class="py-1 px-2">Nam consequat dolor vitae dolor. Donec fringilla. Donec feugiat metus</td>
  135. </tr>
  136. </tbody>
  137. </table>
  138. </div>
  139. </div>
  140. <hr>
  141. <center><button class="btn btn-sm btn-success rounded-0 my-2" class="button" onclick="print_element('printables')">Print Both Element</button></center>
  142. </div>
  143. </body>
  144.  
  145. </html>

Creating the JavaScirip/jQuery Script

The code below is the javascript/jquery script for printing the elements. The function below is being executed when Print Buttons are triggered. Save this file as script.js and include the file in your index.html file.

  1. function print_element($element_id = null) {
  2. if ($element_id == null) {
  3. // Alert Message when function was triggered without a element_id
  4. alert("No Element ID Selected");
  5. } else {
  6. // Cloning the specific element to print
  7. var el = $('#' + $element_id).clone()
  8. // removing the button incuded on the element
  9. el.find('button').remove()
  10. // cloning the element which contains the CSS for styling user-interface
  11. var head = $('head').clone()
  12. // creating a temporary container of the element to print
  13. var new_html = $('<html>')
  14.  
  15. head.find('title').append(' - Print View')
  16. // adding the css to temporary container element
  17. new_html.append(head)
  18. new_html.append('<main></main>')
  19. // adding the element to temporary container element
  20. new_html.find('main').append(el)
  21.  
  22. // Opening a new window (Pop-up) for the print view
  23. var new_window = window.open('', '_blank', 'location=no,width=900,height=700,left=250')
  24. // adding the content of temporary element to the new window
  25. new_window.document.write(new_html.html())
  26. // closing the new window document
  27. new_window.document.close()
  28.  
  29. setTimeout(() => {
  30. // Trigger Print View
  31. new_window.print()
  32. setTimeout(() => {
  33. // Automatically closes the pop window after printing / canceling print view
  34. new_window.close()
  35. }, 200)
  36. }, 200);
  37.  
  38. }
  39. }

That's it! You can now test the source code you created on your end and see if it works as we planned. If there's an error occurred, kindly review the source code I provided above. Working source code is also provided in this article. Download Button is located below.

Result Snapshots

Page Interface

Printing Specific Element

Page Print Preview

Printing Specific Element

DEMO VIDEO

That ends this tutorial. I hope this tutorial will help you with what you are looking for and you'll find this useful for your future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding :)

Add new comment