views:

230

answers:

2

Hi all,

I have the following function.

$(function() {
    $(".sectionHeader:gt(0)").click(function() {
        $(this).next(".fieldset").slideToggle("fast");
    });
    $("img[alt='minimize']").click(function(e) {
        $(this).closest("table").next(".fieldset").slideUp("fast");
        e.stopPropagation();
        return false;
    });
    $("img[alt='maximize']").click(function(e) {
        $(this).closest("table").next(".fieldset").slideDown("fast");
        e.stopPropagation();
        return false;
    });
});

<script type="text/javascript">
     window.onbeforeprint = expandAll;

     function expandAll(){
       $(".fieldset:gt(0)").slideDown("fast");
      }
</script>

For this html

    <table class="sectionHeader" ><tr ><td>Heading 1</td></tr></table>
     <div style="display:none;" class="fieldset">Content 1</div>

 <table class="sectionHeader" ><tr ><td>Heading 2</td></tr></table>
     <div style="display:none;" class="fieldset">Content 2</div>

I have several div class="fieldset" over the page, but when I do print preview or print, I can see all divs sliding down before opening the print preview or printing but on the actual print preview or print out they are all collapse.

I would appreciate if anyone comes with a solution for this.

Anyone have any idea why is this or how to fix it?

Thanks.

PS:Using a does not work either ( I assume because jquery using toggle) and its not the kind of question I am looking for.

+4  A: 

An easier way to do this is in the CSS, you can have different styles for printing vs the screen like this:

@media print {
  .fieldset { display: block; }
}

@media screen {
  .fieldset { display: none; }
}

The .fieldset class will display when printing, but not by default in the browser. Be sure to take out your in-line diplay: none styles on .fieldset divs in the page, as they'll override either of these in a CSS file.

Nick Craver
+1 best and simplest approach
scunliffe
Thanks nick, and the problem with the onbeforeprint event it was because the same, the inline style.Once again, thanks. +1.
Cesar Lopez
+1  A: 

Hi Cesar, I would like to strongly suggest to you not to use this technology for printing. You need to rely on a setup that works without javascript for printing.

Although the method you are going for can be marked as clever although using:

<style type="text/css" media="print">  css to modify for print  </style>

Will ensure that you get your expanded panels for print even without javascript.

XGreen
Thanks for your answer, and I will do, I was just curious of why would it not work the jquery and why it was not working either with css. As Nick Craver says on his answer, its all because the inline style.
Cesar Lopez
I can mention another important benefit that developers should keep in mind that if you have even remotely content that might be of use to disabled (vision disabled) people you need to keep in mind that most screen readers still don't work well with javascript or totally disable it. So all functionality on the client still needs to work fine if you have your js disabled.
XGreen