-1

This question is a little add for this Stackoverflow question Here I ended up with this code.

<script type="text/javascript">
    //Simple wrapper to pass a jQuery object to your new window
    function PrintElement(elem){
    var data = '';
    $(elem).each(function() {
        data = data + $(this).html();
    });
    Popup(data);  
}

    //Creates a new window and populates it with your content
    function Popup(data) {
        //Create your new window
        var w = window.open('', 'Print', 'height=400,width=600');
        w.document.write('<html><head><title>Print</title>');
        //Include your stylesheet (optional)
        w.document.write('<link rel="stylesheet" href="add/css/layout.css" type="text/css" />');
        w.document.write('<link rel="stylesheet" href="add/css/main.css" type="text/css" />');
        w.document.write('</head><body>');
        //Write your content
        w.document.write(data);
        w.document.write('</body></html>');
        w.print();
        w.close();

        return true;
    }
  </script>

that when i tricker the

onclick="PrintElement('.PrintElement')">Print

I can print out some divs with the class="PrintElement" my question is now...

If i have some elements inside the DIV that i dont want to print out, how can i then add a class="NOprintelement" so the code know that the elements with this class, need to be excluded in the print event ?

Community
  • 1
  • 1
Thomas BP
  • 1,187
  • 3
  • 26
  • 62

1 Answers1

1

Without knowing more details, you should probably try to hide DOM elements using css media queries. For example, if you have a div with class = 'hideWhenPrinting', your CSS could include:

@media print {
  .hideWhenPrinting { display: none }
}

See this related question: How do I hide an element when printing a web page?

Community
  • 1
  • 1
Ed Schembor
  • 8,090
  • 8
  • 31
  • 37