Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Created December 19, 2016 11:31
Show Gist options
  • Save andrewlimaza/490a69417d9fe2df3f668195a7661605 to your computer and use it in GitHub Desktop.
Save andrewlimaza/490a69417d9fe2df3f668195a7661605 to your computer and use it in GitHub Desktop.
Print certain div / elements using window.print()
<script>
function printDiv(divName){
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
</script>
<h1> do not print this </h1>
<div id='printMe'>
Print this only
</div>
<button onclick="printDiv('printMe')">Print only the above div</button>
@VidhyaBascaran
Copy link

@azeenGAN Thank you so much. I still can't get the desired alignment, is there any other options to do?

@SEENIVASANIT
Copy link

I set @page size 2in,3in but is only work chrome not other? If any idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment