denny denny - 4 months ago 10
AngularJS Question

print the content of a HTML DIV

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?

Answer

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