How to Print Div Contents in Php

QuestionsCategory: PHPHow to Print Div Contents in Php
Ishu asked 2 years ago

When Going to print using CTRL+P ,

It prints the whole body html contents.

But I want to print only a particular div content. Do not print the other content outside the div.

1 Answers
Best Answer
devchandan Staff answered 2 years ago

If You want to print the section or Div contents, then please do the following…
By using the following code you can print the contents in a div or section.

<!-- For Print the Contents Starts -->

<script language="javascript" type="text/javascript">

function printDivContents(divID) {

//Get the HTML of div
var divElements = document.getElementById(divID).innerHTML;

//Get the HTML of whole page
var oldPage = document.body.innerHTML;

//Reset the page's HTML with div's HTML only
document.body.innerHTML = 
"<html><head><title></title></head><body>" + 
divElements + "</body>";

//Print Page

//Restore orignal HTML
document.body.innerHTML = oldPage;


<!-- For Print the Contents / Ends-->

Please Check.. the solution here.. Print Div Contents