Print a div with javascript

Hello Friends. Sometimes yoy want to print contents of only a div and not the whole window. Use this little javascript function.

Just click on the print button and the contents of the div will be printed.

The Javascript

 <script type="text/javascript">    
 function PrintThis() {   
  var printThisDiv=document.getElementById('print-cntnt');
  popup.document.write('<html><body onload="window.print()">'
          + printThisDiv.innerHTML + '</html>');

The Html

  <div id="print-cntnt">
      Contents of the div to be to printed !!
 <input type="button" value="Print" onClick="PrintThis();">


Print a div with javascript Print a div with javascript Reviewed by Altaf Hussain on 3:19 PM Rating: 5

1 comment:

  1. That is the one that worked for me. Iekpt the script part at the end of the file.


Altaf Web. Powered by Blogger.