Create a Web print button that omits certain items on the page

Ever want to have a print button on your Web page that only prints certain parts of the page and omits other parts? This can easily be done in Notes with a Hide/When for printing. Here's a way to do it with javascript on the Web.

Simply enclose the parts of the page you wish not to print with the following:

<div id="divNoPrint1">
Dont print this line

Of course the "Dont print this line" is my sample text, and the <div> elements need to be pass-thru HTML.

As you add multiple DIV statements to omit other parts of your page increment the div number (ie "divNoPrint2" and "divNoPrint3" etc.. in place of the divNoPrint1 example above)

Then add the following function to JS Header of the form.

function printPage() {
var numDivs = 20;
for(i=1;i<numDivs;i++) {
    document.all['divNoPrint'+i].style.display = 'none'; }
for(i=1;i<numDivs;i++) {
    document.all['divNoPrint'+i].style.display = ''; }

This function above only supports 20 items, but of course you can change the divNums variable to be a higher number if necessary.

Then add a print button to your page, form, or view template as such:

<div id="divNoPrint3" align=right><a href="#" 
onClick="printPage();"><img src="/icons/actn147.gif" border=0 
alt="Print page"></a></div>

Of course make sure the above code for the print button is pass-thru HTML. (And note how it is using the name "divNoPrint3" so that it wont be printed.)

And thats it. When the print button is clicked the page will appear to jump for a second (hiding the sections that are not to be printed) and then the print dialog box will come up and the page will return to normal.

This was first published in December 2001

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.