Misam
Misam

Reputation: 4389

printing a div using jQuery

How do I just print the article on my web page using jQuery, article is inside a div window.print() will print the entire window. So little confused with that...!

Upvotes: 0

Views: 5647

Answers (3)

Graham
Graham

Reputation: 15244

Styling html for printing is commonly achieved using a different style sheet, e.g. in your head element:

<head>
    ...

    <link rel="stylesheet" type="text/css" media="screen" href="style.css">
    <link rel="stylesheet" type="text/css" media="print" href="print.css">
    ...

</head>

The style sheet style.css is used to style your html on screen, while print.css is used to style your html for print. By providing the appropriate styling in print.css you can easily show or hide html elements to achieve the output you are looking for.

Upvotes: 1

Martin
Martin

Reputation: 11041

Don't use jQuery ... use css and media=print. Here is an article for reference, and here.

Basically, create a new stylesheet for what you want to show when you print, and set the media to print:

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

Upvotes: 5

Henrik N
Henrik N

Reputation: 16294

You could probably put the content in an iframe and call print() on that window.

A quick google gave me http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=449 which mentions you must focus() before you print(). Note that that page is a few years old.

Also see How do I print an IFrame from javascript in Safari/Chrome.

Upvotes: 1

Related Questions