I am working on a page, where the user may click a button and print some important information on the page.
Basic page structure is like:
<body>
<div id="printContent"></div>
<button>Print</button>
</body>
And the desired behavior is: when the page is being printed, only the contents inside "printContent" should be printed.
Before I used js code to do it by temporarily replacing the body with whatever is inside printContent, but this seems to slow down the page a bit when user gets out of the printing options pop-up. (The temporary content will occupy the whole page for a few seconds or some of the buttons and links become non-responsive for a few seconds.) So after referring to the second answer here:How do I print part of a rendered HTML page in JavaScript?
I used something like this:
@media print {
body * {
display:none;
}
body #printContent {
display:block;
}
}
But when printing, the printContent part is still invisible. Did I do anything wrong here? Thanks.
EDIT: My bad, it is not the second answer from the link, the second "Highest upvoted answer", namely the one with 15 upvotes.