I have a modal popup containing a div. I have a button to let the user print the content of that div. window.print prints the whole page. how can I print only the contents of the div.
Thanks.
use this script
<script language="javascript" type="text/javascript">
function CallPrint(strid) {
var prtContent = document.getElementById(strid);
var WinPrint = window.open('', '', 'letf=0,top=0,width=800,height=100,toolbar=0,scrollbars=0,status=0,dir=ltr');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
prtContent.innerHTML = strOldOne;
}
</script>
and call it from button
<asp:button id="BtnPrint" runat="server" onclientclick="javascript:CallPrint('bill');" text="Print" xmlns:asp="#unknown" />
your div also should has name (bill)
This code may help u..
HTML Code:
<div id="printarea">
//content you want to print
</div>
<input id="btnprint" type="button" onclick="PrintDiv()" value="Print" /></center>
This is javascript
<script type="text/javascript">
function PrintDiv() {
var divToPrint = document.getElementById('printarea');
var popupWin = window.open('', '_blank', 'width=300,height=400,location=no,left=200px');
popupWin.document.open();
popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
popupWin.document.close();
}
</script>
try this:
function printdiv(printpage)
{
var headstr = "<html><head><title></title></head><body>";
var footstr = "</body>";
var newstr = document.all.item(printpage).innerHTML;
var oldstr = document.body.innerHTML;
document.body.innerHTML = headstr+newstr+footstr;
window.print();
document.body.innerHTML = oldstr;
return false;
}
printpage
argument is your div id.
source: Microsoft forums: http://forums.asp.net/t/1261525.aspx/1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With