Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

What's the best way to print the contents of a DIV?

1 Answer

0 votes
by (40.7k points)

Try using the code given below:

function PrintElem(elem)

{

    var mywindow = window.open('', 'PRINT', 'height=400,width=600');

    mywindow.document.write('<html><head><title>' + document.title  + '</title>');

    mywindow.document.write('</head><body >');

    mywindow.document.write('<h1>' + document.title  + '</h1>');

    mywindow.document.write(document.getElementById(elem).innerHTML);

    mywindow.document.write('</body></html>');

   mywindow.document.close(); // necessary for IE >= 10

    mywindow.focus(); // necessary for IE >= 10*/

    mywindow.print();

    mywindow.close();

   return true;

}

Note: The above code is tested on CHROME.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...