Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print only a selected HTML element?

I am trying to implement a print feature in HTML. I know I can print the whole page with window.print(), but how do I print only a specific page element? For example a particular <DIV>Some text to print</DIV>.

like image 872
t0mcat Avatar asked Jun 28 '11 02:06

t0mcat


2 Answers

You could use a print specific CSS stylesheet and hide everything but what you want printed.

<div class="no-print">I won't print</div><div class="something-else">I will!</div>

Just the no-print class will be hidden, but anything with a print class will show.

<style type="text/css" media="print">    .no-print { display: none; } </style> 
like image 74
ashurexm Avatar answered Oct 08 '22 10:10

ashurexm


If you are familiar to jQuery, you can use jQuery Print Element plugin like this:

$('SelectorToPrint').printElement(); 
like image 31
Ravan Scafi Avatar answered Oct 08 '22 11:10

Ravan Scafi