Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print DIV content by JQuery

suppose i have many div's in my page but i want to print the content of specific div using jquery. i found there is a plugin for that.

jQuery Print Element

using this plugin we can easily print div content like below.

$('SelectorToPrint').printElement();

but what happen if my div is hidden in page. so then does it work. this plugin can print the content of hidden div?

what happen if printer is not attach with client machine. i want to show message if printer is not there to customer that "Printer not found"? how to handle this situation. so please advise me what would be the best approach to print the content of hidden div in page and as well as handle printer issue if printer is not attached.

thanks

like image 201
Keith Costa Avatar asked Sep 23 '11 14:09

Keith Costa


People also ask

How to print the content of a div?

To print the content of div in JavaScript, first store the content of div in a JavaScript variable and then the print button is clicked. The contents of the HTML div element to be extracted.

How do I print something in jQuery?

jQuery Code:$('#sudo). click(function(){ window. print(); return false; });

How to get the content of a div in JavaScript?

To get the content of div which contains JavaScript script blocks, use the html() method. You can try to run the following code to get the content. With html(), get the content of div, which includes the JavaScript script.


2 Answers

I prefer this one, I have tested it and its working

https://github.com/jasonday/printThis

$("#mySelector").printThis();

or

$("#mySelector").printThis({
*      debug: false,              * show the iframe for debugging
*      importCSS: true,           * import page CSS
*      printContainer: true,      * grab outer container as well as the contents of the selector
*      loadCSS: "path/to/my.css", * path to additional css file
*      pageTitle: "",             * add title to print page
*      removeInline: false        * remove all inline styles from print elements
*  });
like image 139
Bishoy Hanna Avatar answered Sep 22 '22 20:09

Bishoy Hanna


If a hidden div can't be printed with that one line:

$('SelectorToPrint').printElement();

simply change it to:

$('SelectorToPrint').show().printElement();

which should make it work in all cases.

for the rest, there's no solution. the plugin will open the print-dialog for you where the user has to choose his printer. you simply can't find out if a printer is attached with javascript (and you (almost) can't print without print-dialog - if you're thinking about that).

NOTE:

The $.browser object has been removed in version 1.9.x of jQuery making this library unsupported.

like image 36
oezi Avatar answered Sep 23 '22 20:09

oezi