0

I want to print the contents of HTML. For this I have created a function and in this function I have passed the div ID in it. But the choose shown the following message.

enter image description here

My javascript is under document.ready function

function printDiv(divName) {
  var printContents = document.getElementById(divName).innerHTML;
  var originalContents = document.body.innerHTML;
  document.body.innerHTML = printContents;
  window.print();
  document.body.innerHTML = originalContents;
}

And here I am getting error Unused function printDev

I have tried to enable NodeJS and NPM but still no use

Any help would be highly appreciated.

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Moeez
  • 494
  • 9
  • 55
  • 147

1 Answers1

1

My javascript is under document.ready function

If you defined the printDiv function inside another function, it's not visible to your onclick HTML parameter.

You can either define it as a global function, or add an eventListener to your element inside your document.ready function :

function printDiv(divName) {
       var printContents = document.getElementById(divName).innerHTML;
       var originalContents = document.body.innerHTML;
       document.body.innerHTML = printContents;
       window.print();
       document.body.innerHTML = originalContents;
}

$( document ).ready(function() {
    document.getElementById("printButton").addEventListener("click", function() {
        printDiv('print');
    });
});
<!DOCTYPE html>
<html lang="en">
<head>
  <script src="https://code.jquery.com/jquery-2.2.4.js"></script>
</head>
<body>
  <input type="button" id="printButton" value="Print PDF"></input>
  <div id="print"></div>
</body>
</html>
Guillaume Georges
  • 3,878
  • 3
  • 14
  • 32