views:

140

answers:

4

Is there any solution to disable Javascript style changes in print?

For instance, if I'm hiding something through Javascript but I want to include that hidden info in print.

I hid a div using Javascript and I want to show that div if Javascript is disabled. Now the problem is, because div is hidden using Javascript it's also not showing when the page is printed.

+1  A: 

Use a print stylesheet, along with !important statements to force the element to be visible for printing.

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

CSS:

#myDiv { display: block!important; } 
Andy E
Cool it's working - Great Help.
metal-gear-solid
A: 

I suggest you take a look at this article: CSS Design: Going to Print.

Grz, Kris.

XIII
That's a generic introduction to the subject of print stylesheets that does not cover the issue described in the question.
David Dorward
And a very good one which can lead to a better insight to the OP and future readers of this thread.
XIII
I've already read this article.It's not new for me
metal-gear-solid
Which I didn't know and I only tried to help you out with some good information.
XIII
@XII - I appreciate your effort. Thanks
metal-gear-solid
+1  A: 

The use of !important has already been mentioned, but it is a blunt instrument and things get very complicated once you start needing to override things which are already !important.

One of the great benefits of CSS is that it allows you to separate style from structure. Instead of using JS to manipulate the style of an element, use it to manipulate the structure. For example, by manipulating the className property.

Your screen media stylesheet can then hide the element, while leaving it visible for the print media stylesheet.

This has the additional benefit that you don't need to think about having to override these styles as they won't apply in the first place (for print).

David Dorward
you mean i should add separate class to `div` to show in print. can you explain in detail
metal-gear-solid
@met: create a class like `visible-in-print` which is styled to `display: none` for `screen` and add that class to elements you want to hide instead of using inline styles.
Joey
but i can't keep `display:none` in css. I'm hiding as this `document.write('<style type="text/css" media="screen">#sitemapContainer { display: none; }</style>');`
metal-gear-solid
A: 

Have a look at this one mate http://atti.me.uk

Atti