Print the content of a div element using JavaScript Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 4 Likes Like Report If you want to print the content of a <div> element using JavaScript, you can achieve this by extracting the content and then opening a new window or popup to display and print it. This method involves capturing the content of the <div>, creating a new window, and using the print command to print the content.Example 1: This example uses JavaScript window print command to print the content of div element. html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Print Div Content</title> </head> <body style="text-align:center;"> <div id="GFG" style="background-color: green; padding: 20px;"> <h2>GeeksforGeeks</h2> <p>This content inside the div will be printed when you click the button.</p> </div> <input type="button" value="Print Div" onclick="printDiv()"> <script> function printDiv() { let divContents = document.getElementById("GFG").innerHTML; let printWindow = window.open('', '', 'height=500, width=500'); printWindow.document.open(); printWindow.document.write(` <html> <head> <title>Print Div Content</title> <style> body { font-family: Arial, sans-serif; } h1 { color: #333; } </style> </head> <body> <h1>Div Contents:</h1> ${divContents} </body> </html> `); printWindow.document.close(); printWindow.print(); } </script> </body> </html> Output:Example 2: This example uses the JavaScript window print command to print the content of div element. html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Print Div Content with Table</title> </head> <body> <div id="GFG" style="background-color: green; padding: 20px;"> <h2>GeeksforGeeks</h2> <table> <tr> <th>Item</th> <th>Description</th> </tr> <tr> <td>Computer</td> <td>Algorithm</td> </tr> <tr> <td>Microwave</td> <td>Infrared</td> </tr> </table> </div> <p>The table inside the div will be printed on button click.</p> <input type="button" value="Print Div" onclick="printDiv()"> <script> function printDiv() { let divContents = document.getElementById("GFG").innerHTML; let printWindow = window.open('', '', 'height=500, width=500'); printWindow.document.open(); printWindow.document.write(` <html> <head> <title>Print Div Content</title> <style> body { font-family: Arial, sans-serif; } table { width: 100%; border-collapse: collapse; } th, td { border: 1px solid #ddd; padding: 8px; } th { background-color: #f4f4f4; } </style> </head> <body> <h1>Div Contents:</h1> ${divContents} </body> </html> `); printWindow.document.close(); printWindow.print(); } </script> </body> </html> Output:JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples. Create Quiz Comment A adeshsingh1 Follow 4 Improve A adeshsingh1 Follow 4 Improve Article Tags : JavaScript Web Technologies Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like