简体   繁体   中英

unresolved function or method JavaScript yii2

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.

在此处输入图片说明

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.

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM