1

I have particular division in my web page. i want to print that div only when click "print" in my web page. i have some javascript code. But that is not well formatted.

Is there any javascript to print particular division from my web page?

Thanks in advance

  • Gnaniyar Zubair
1
  • Do you mind marking this 'Answered'? Commented Jul 22, 2009 at 18:34

5 Answers 5

9

You can specify CSS stylesheets with the attribute media="print" and apply the style display: none; to all elements except those which you want printed.

For example:

print.css

* { display: none; }
div.print_block { display: block; }

page.html

<HEAD>
<LINK rel="stylesheet" type="text/css" 
     media="print" href="print.css" />
</HEAD>

<DIV class="print_block">
   ...
</DIV>
Sign up to request clarification or add additional context in comments.

3 Comments

thanks andy... then in print button onclick i have to give onclick='window.print()'. then it will print only that div. right?
Yes, though the JavaScript isn't really relevant; the same will happen when the user goes to File->Print themselves.
Yep. It applies the media="print" styles. You can test this without printing by changing the tag to media="screen" instead (making sure it's the last style sheet referenced.
5

If you want to hide some divs when printing, you can set them to "display: none" in a print media section of your stylesheet, and they won't appear.

eg, in your stylesheet:

 @media print {

    div.header {
        display: none;
    }

    div.printable {
        page-break-inside: avoid;
        page-break-after: always;
    }
 }

This answer is almost exactly the same as the two which beat me by 4 minutes :-) , just a note that you don't need a whole separate stylesheet if you don't want to ...

Also, the "page-break-inside: avoid;" and "page-break-after: always;" clauses are good for printing out a few divs, each on its own page.

Comments

3

Check this jQuery plugin jqPrint. It seems to do what you want.

Comments

1

I don't think there is a direct way of doing this. You can however:

  • Create a special CSS for media=print where all but the one div you want to print is hidden, OR
  • Use a hidden iframe for the content you want to print

Googling with the terms "javascript print partial page" may help you find some tutorials.

2 Comments

CSS would be the better option.
Sure, but I have seen pages use iframes as well, mentioned it for the sake of completeness.
0

I saw smf like that in one project at my job. You have to open popup in popup you put content from parent page which you wanna to print. And it's all :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.