2

I have created four apex tabs in a vf page. In that page,I'm using print functionality for the particular div section.Its works fine when i click the print button for the first time.But when i click the second tab page gets refreshed.How can i resolve this issues.enter image description here

function printDiv(divID) {
            //Get the HTML of div
            var divElements = document.getElementById(divID).innerHTML;
            //Get the HTML of whole page
            var oldPage = document.body.innerHTML;
            //Reset the page's HTML with div's HTML only
            document.body.innerHTML = 
              "<html><head><title></title></head><body>" + 
              divElements + "</body>";

            //Print Page
            window.print();

            //Restore orignal HTML
            document.body.innerHTML = oldPage;
        }
                <input type="button" value="Print" onclick="return printDiv('printablediv');" style="width:80px; height:50;font-size: 18px; " />

<div=printDiv>
<apex:tab>
text here
</apex:tab>
<apex:tab>
text here1
</apex:tab>
</div>

CSS Code:

<style type="text/css" media="print">
@media print
{
#non-printable { display: none; }
#printable {
display: block;
width: 100%;
height: 100%;
}
}
</style>
</head>
<body>
<div id="printable" >
Your content to print
</div>
<input type="button" id="non-printable" class=normaltext onclick="JavaScript:window.print();" value="print" />

2 Answers 2

0

I think your issue should be solved by having your function to return false.

In regards to your printing functionality, I would use CSS to print a specific section rather than JavaScript.

1
  • Thank for your reply. I have tried the code and also updated my question. It's printing the full page but not printing the particular div. Commented Feb 25, 2014 at 9:24
0

window.print() is a browser native function for printing the page. You'll need to implement an external library like jsPDF to print a particular element from a page. The library includes examples which can help you achieve what you are hoping to implement.

You may also find this resource helpful if you want to continue to use the .print() function. MDN Print Guide

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .