Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print the contents of a DIV

Whats the best way to print the contents of a DIV?

like image 233
usertest Avatar asked Feb 12 '10 21:02

usertest


People also ask

How do I print a specific area in HTML?

You can use this function for print a specific area of web page or full web page content. Use printPageArea() function on onclick event of print button element and provide the content area div ID which you want to print.

How do you print HTML content on click of a button but not the page?

One of those solutions are: I can keep this HTML content in a div and make it display: to print, but display: none to screen. All other elements on the webpage can be made to display: none for print and display: for the screen. And then call to print.

How do you print a paragraph in JavaScript?

JavaScript does not have any print object or print methods. You cannot access output devices from JavaScript. The only exception is that you can call the window.print() method in the browser to print the content of the current window.

How do I print a Web page using JavaScript?

To print a page in JavaScript, use the print() method. It opens up the standard dialog box, through which you can easily set the printing options like which printer to select for printing.


1 Answers

Slight changes over earlier version - tested on CHROME

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; } 
like image 127
Bill Paetzke Avatar answered Sep 23 '22 10:09

Bill Paetzke