Printing HTML DIV tag content using JavaScript

Introduction

Open Visual Studio and create new project and select Web template from the list and select ASP.NET Empty Web Application. Enter the name of your application and click on OK.

Right click on the project and select Add -> Web Form and name it as Home.aspx.

Now paste the below JavaScript code in the head section of the Home.aspx page.

  1. <script type="text/javascript">  
  2.     function PrintDiv() 
  3.    {  
  4.        var divContents = document.getElementById("printdivcontent").innerHTML;  
  5.        var printWindow = window.open('''''height=200,width=400');  
  6.        printWindow.document.write('<html><head><title>Print DIV Content</title>');  
  7.        printWindow.document.write('</head><body >');  
  8.        printWindow.document.write(divContents);  
  9.        printWindow.document.write('</body></html>');  
  10.        printWindow.document.close();  
  11.        printWindow.print();  
  12.     }  
  13. </script>  

Home.aspx Page Code

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="PrintContentofHTMLDIV.Home" %>
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head>  
  7.     <title>Home</title>  
  8.     <script type="text/javascript">  
  9.         function PrintDiv() {  
  10.             var divContents = document.getElementById("printdivcontent").innerHTML;  
  11.             var printWindow = window.open('''''height=200,width=400');  
  12.             printWindow.document.write('<html><head><title>Print DIV Content</title>');  
  13.             printWindow.document.write('</head><body >');  
  14.             printWindow.document.write(divContents);  
  15.             printWindow.document.write('</body></html>');  
  16.             printWindow.document.close();  
  17.             printWindow.print();  
  18.         }  
  19.     </script>  
  20. </head>  
  21. <body>  
  22.     <form id="form1">  
  23.     <hr />  
  24.     <div id="printdivcontent" style="border: 1px dotted black; text-align: center; padding: 5px; width: 300px">  
  25.         <p style= "text-align:justify; ">  
  26.             JavaScript is the programming   
  27.             language of HTML and the Web. <br />  
  28.             Programming makes computers do what you want them to do.  
  29.             <br />  
  30.             JavaScript is easy to learn.  
  31.         </p>  
  32.     </div>  
  33.     <br />  
  34.     <input type="button" onclick="PrintDiv();" value="Print" />  
  35.     </form>  
  36. </body>  
  37. </html>  
Explanation

The contents of the HTML DIV tag is extracted when Print button is clicked. The extracted content of the HTML DIV are written to the JavaScript popup window and finally using the JavaScript Windows Print Command the window is printed.

Screenshots

home page 
 
print divcontent  
 
print dialog 
Ebook Download
View all
Learn
View all