How do you print html content on click of a button but not the page?

I want to print some HTML content, when the user clicks on a button. Once the user clicks on that button, the print dialog of the browser will open, but it will not print the webpage. Instead, it will print the some other HTML content which is not displayed on the page.

While asking this question, there are few solutions coming into my mind. But I am not sure whether those are good ideas or something better can be done. One of those solutions are: I can keep this HTML content in a div and make it display: to print, but display: none to screen. All other elements on the webpage can be made to display: none for print and display: for the screen. And then call to print.

Any better idea?

asked Jun 3, 2013 at 10:28

3

@media print {
  .noPrint{
    display:none;
  }
}
h2{
  color:#f6f6;
}

print me

no print

I came across another elegant solution for this:

Place your printable part inside a div with an id like this:

Print me

Now let's create a really simple javascript:

function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

SOURCE : SO Answer

answered Jun 3, 2013 at 11:05

How do you print html content on click of a button but not the page?

asprinasprin

9,47111 gold badges67 silver badges112 bronze badges

11

Here is a pure css version

.example-print {
    display: none;
}
@media print {
   .example-screen {
       display: none;
    }
    .example-print {
       display: block;
    }
}
You only see me in the browser
You only see me in the print

How do you print html content on click of a button but not the page?

Jonathan

2,3254 gold badges22 silver badges37 bronze badges

answered Jun 3, 2013 at 10:36

2ne2ne

5,5489 gold badges29 silver badges49 bronze badges

3

According to this SO link you can print a specific div with

w=window.open();
w.document.write(document.getElementsByClassName('report_left_inner')[0].innerH‌​TML);
w.print();
w.close();

answered Jun 3, 2013 at 10:35

How do you print html content on click of a button but not the page?

JaayJaay

2,0731 gold badge15 silver badges33 bronze badges

6

I Want See This

Example http://jsfiddle.net/35vAN/

    

 




This will be printed. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a quam at nibh adipiscing interdum. Nulla vitae accumsan ante.
This will not be printed.
Nor will this.

answered Jul 25, 2014 at 12:27

How do you print html content on click of a button but not the page?

1

Below Code May Be Help You :






Block 1
1XYZathock
This is Block 2 content

answered Jun 3, 2013 at 10:36

How do you print html content on click of a button but not the page?

Butani VijayButani Vijay

4,0902 gold badges28 silver badges60 bronze badges

1

If you add and remove the innerHTML, all javascript, css and more will be loaded twice, and the events will fire twice (happened to me), is better hide content, using jQuery and css like this:

function printDiv(selector) {
    $('body .site-container').css({display:'none'});
    var content = $(selector).clone();
    $('body .site-container').before(content);
    window.print();
    $(selector).first().remove();
    $('body .site-container').css({display:''});
}

The div "site-container" contain all site, so you can call the function like:

printDiv('.someDiv');

answered Jul 4, 2014 at 0:26

Alejo JMAlejo JM

8917 silver badges12 bronze badges

1

How do I print a certain part of a web page in HTML?

You can use this function for print a specific area of web page or full web page content. Use printPageArea() function on onclick event of print button element and provide the content area div ID which you want to print.

How do I print only certain parts of a website?

Only Print Text You Highlight on a Page Highlight the text and/or images you want to print on a web page. Now in your browser go to File > Print or simply use the Ctrl + P keyboard combination. The Print screen comes up. Select the Printer you want to use.

How do I hide an element when printing a web page?

To hide the element, add “display:none” to the element with CSS.

How do I print a button click page?

You can easily add a print button to your web page by adding the following code to your HTML document where you want the button to appear:.
onclick="window.print();return false;" />.
print..
type="text/css" media="print" />.
body {visibility:hidden;} .print {visibility:visible;}.