Saturday, October 23, 2021

Print the Content of Div or Element

 There are several ways to print the div or section, however I found this is easiest way and it works fine.

Code Sample

function printDiv(divID) {
     var printContents = document.getElementById(divID).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

Let me a bit explanation of the code. First we are selecting the contents of div as well as full body. Before print call, we are assigning the div contents to body and doing print. Finally, we are restoring the original body contents.

No comments:

Post a Comment

No String Argument Constructor/Factory Method to Deserialize From String Value

  In this short article, we will cover in-depth the   JsonMappingException: no String-argument constructor/factory method to deserialize fro...