Printing a Page or Specific Element Data using JavaScript - window.print() method
JavaScript Function in Javascript (Article) Function in Javascript (Program)
160
In this section you will learn how to print particular element from a webpage. Here we have written one function myPrintfunction to print a paragraph.
Program:
<!DOCTYPE html> <html> <body> <h2>JavaScript Functions to print Specific Element With Id - atnyla.com</h2> <p>I Don't want to print this text.</p> <p id="textAreaToPrint"> This is the text I want to print. </p> <button type="button" onclick="myPrintfunction()" class="btn btn-sm btn-outline-primary m-1"> Print </button> <script> function myPrintfunction(){ var backup = document.body.innerHTML; var divcontent = document.getElementById("textAreaToPrint").textContent; document.body.innerHTML = divcontent; window.print(); document.body.innerHTML = backup; } </script> </body> </html>
Output:
Live Preview
![]()