So I have a page with a lot of text and a table on it for example.
I want users to be able to print just the table with a 'print this table' javascript link, but I still want the rest of the page to be printable via the normal browser print method.
So to start with I have a print.css stylesheet included as below:
<link type="text/css" rel="stylesheet" href="style/print.css" media="print">
In print.css I have a class '.noPrint' set to display: none.
It seems to me that the solution is to wrap the content that is not a table in divs with a 'tempNoPrint' class, and on clicking the 'print this table' have javascript add 'noPrint' to all of the divs with 'tempNoPrint' thus hiding them in the printer version.
That's fine and I'm sure would work well.
However, how do I go about removing the 'noPrint' class from all the 'tempNoPrint' divs after the printing of just the table is done? Is there a javascript callback sent from the print dialogue? I could use a timer but it seems that would be very unreliable.
So I have a page with a lot of text and a table on it for example.
I want users to be able to print just the table with a 'print this table' javascript link, but I still want the rest of the page to be printable via the normal browser print method.
So to start with I have a print.css stylesheet included as below:
<link type="text/css" rel="stylesheet" href="style/print.css" media="print">
In print.css I have a class '.noPrint' set to display: none.
It seems to me that the solution is to wrap the content that is not a table in divs with a 'tempNoPrint' class, and on clicking the 'print this table' have javascript add 'noPrint' to all of the divs with 'tempNoPrint' thus hiding them in the printer version.
That's fine and I'm sure would work well.
However, how do I go about removing the 'noPrint' class from all the 'tempNoPrint' divs after the printing of just the table is done? Is there a javascript callback sent from the print dialogue? I could use a timer but it seems that would be very unreliable.
Share Improve this question asked Apr 5, 2012 at 10:18 bbeckfordbbeckford 4,4776 gold badges37 silver badges48 bronze badges 4- 1 Have you tried removing the noPrint class after sending the data to the printer? I don't think you need a callback here. – Chibuzo Commented Apr 5, 2012 at 10:23
- archive.plugins.jquery./project/printElement – Stefan Commented Apr 5, 2012 at 10:26
- Thanks Chibuzo, how silly of me! I tried that and it works! I've added a full answer. – bbeckford Commented Apr 6, 2012 at 7:13
- Thanks Stefan, but for what I'm doing I wanted to keep the header and footer of the page. – bbeckford Commented Apr 6, 2012 at 7:13
3 Answers
Reset to default 4This might be a bit of a nuclear approach but what I've done previously when tripping over this sort of thing is to output the bit I want to print to a hidden iframe and printing that.
//build new document
var code = "<!doctype html><html><head>";
//add in CSS needed by the table
code += "<link rel='stylesheet' type='text/css' href='table.css' /></head><body>";
//get and add in table code
var code += "<table>"+document.getElementById('some_table').innerHTML+"</table>";
//finish up new doc code
code += "</body></html>";
//write new doc to hidden iframe (name: hiddenFrame)
var doc = hiddenFrame.document.open("text/html","replace");
doc.write(code);
doc.close();
//print
hiddenFrame.print();
It seems I was overplicating the issue!
I wrapped all the content on the page that I didn't want to print with the button divs with a 'noPrintCustom' class, then I call this function:
var printCustom = function() {
$('.noPrintCustom').addClass('noPrint');
window.print();
$('.noPrintCustom').removeClass('noPrint');
}
Thanks to all that helped!
To trigger a callback after the print
dialog exits, you can listen for the afterprint event, e.g.
addEventListener("afterprint", (event) => {
// do things once print dialog has exited
});
Related - there is also a beforeprint event.