Reputation: 9445
JavaScript
<script type="text/javascript">
function printDiv() {
var printContent1 = document.getElementById('divMail1');
var printContent2 = document.getElementById('divMail2');
var newWin = window.open('', 'PrintWindow', 'width=800,height=530,top=10,left=100,scrollbars=1,resizable=yes');
newWin.document.open();
newWin.document.write('<html/><body onload="window.print()">' + printContent1.innerHTML + '</body></html>');
newWin.document.close();
}
</script>
HTML
<div id="divMail1">
<div>
<img src="image1.jpg"/>
</div>
<div>
<img src="image2.jpg"/>
</div>
<div id="divMail2">
<div>
<img src="image3.jpg"/>
</div>
<div>
<img src="image4.jpg"/>
</div>
</div>
<input type="button" value="Print" onclick="printDiv()"/>
Hi, I want to show one div(divMail1) in print preview and print a different div(divMail2). is it possible. please help me.
Upvotes: 0
Views: 959
Reputation: 2203
use css instead
#divMail1 {
display: block;
}
#divMail2 {
display: none;
}
@media print {
#divMail1 {
display: none;
}
#divMail2 {
display: block;
}
}
Upvotes: 0
Reputation: 6887
Do it only with CSS
<head>
<style type="text/css">
#printable { display: none; }
@media print
{
#non-printable { display: none; }
#printable { display: block; }
}
</style>
</head>
<body>
<div id="non-printable">
Your normal page contents
</div>
<div id="printable">
Printer version
</div>
</body>
Upvotes: 2
Reputation: 167212
Instead of using JavaScript, you can use print.css
by adding a media="print"
. So, in your main CSS, you need to add this:
.print-only {display: none;}
.do-not-print {display: block;}
And in the print.css
:
.print-only {display: block;}
.do-not-print {display: none;}
In your case, the code in your main CSS would be:
#divMail1 {display: block;}
#divMail2 {display: none;}
And in the print.css
:
#divMail1 {display: none;}
#divMail2 {display: block;}
Upvotes: 1