Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print the content of a HTML DIV [duplicate]

Tags:

I want to print a content of a html div and currently i am using this code.

<div id="content">
</div>

var printContents = document.getElementById("content").innerHTML;
var originalContents = document.body.innerHTML;

document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;

printing is working correctly. But after that my other functions are stop responding. I'm using angular.js.

any idea?

like image 673
denny Avatar asked Aug 02 '16 04:08

denny


1 Answers

Here is the answer I found

HTML and JS CODE

 <div id="printable">
    Print this div
  </div>
  <button ng-click="printDiv('printableArea');">Print Div</button>


$scope.printDiv = function(divName) {
  var printContents = document.getElementById(divName).innerHTML;
  var popupWin = window.open('', '_blank', 'width=300,height=300');
  popupWin.document.open();
  popupWin.document.write('<html><head><link rel="stylesheet" type="text/css" href="style.css" /></head><body onload="window.print()">' + printContents + '</body></html>');
  popupWin.document.close();
} 

I found it from here

like image 53
The Mechanic Avatar answered Sep 28 '22 04:09

The Mechanic