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.

By Rijwan Ansari

MVP, CSM, CSPO, MCP, MCTS. Sr. Software Developer. Expertise in ASP.NET MVC, Angular, AngularJs, .Net Core, C#.NET, Azure, Cognitive Services, Machine Learning, SharePoint, Azure Services, and tools, Requirement Analysis, Team Management, Pre-Sales and more. Ability to work independently and Team, handle pressure and meet commitment. Excellence Communication and adjust in any environment

Leave a Reply

Your email address will not be published. Required fields are marked *