How to Print specific section of a web page using javascript

In this post, I would like to show how to print a specific section of a web page. Many times we needed to print the section as per our requirements for example- if we have employee details and his/her salary details in the same page and we want only the print of salary details section then we can get this through javascript.

Here is i am defining two div section with unique ID. One is for employee details and other is for salary details. And the div ID passes from the click function of anchor tag through to the javascript function. And in the javascript function we can get html content through innerhtml property and can print it. Please find below the code.


<title>Print specific section example</title>
 @media print {
      display: none !important;
<div id="employee_details" >
    <table align="center" cellspacing = "0" style="background-color:lightgrey;" width="80%" border="1">
    <tr><td>Employee Details</td><td colspan="3" align="right"><a href="javascript:void(null);" onclick="print_section('employee_details')" class="noprint">Print</a></td></tr>
			<td><label>Mr. Ajay Trivedis</label></td>
			<td>Mobile: </td>
			<td>Adress: </td>
<div id="payment_details" >
    <table align="center" cellspacing = "0" style="background-color:#d09d9d;" width="80%" border="1">
    <tr><td>Salary Details</td><td colspan="4" align="right"><a href="javascript:void(null);" onclick="print_section('payment_details')"  class="noprint">Print</a></td></tr>
			<td>Date: </td>
			<td>Gross: </td>
			<td>Net Payout: </td>
<script type="text/javascript">
    function print_section(section) {
        var printContents = document.getElementById(section).innerHTML;
        var originalContents = document.body.innerHTML;
        document.body.innerHTML = printContents;
        document.body.innerHTML = originalContents;

Print Specific Section

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – let us know. We’d love to help!

I am Hitesh from Jamshedpur (India). I have been working as a Web Application Developer from last 4+ years. I love diverse and attention catching web presence for a variety of users. Also I love to learn new things in Web Development.

Tags: , ,
error: Content is protected !!
Secured By miniOrange