Skip to content

Instantly share code, notes, and snippets.

@grim-reapper
Forked from andrewlimaza/example.html
Created April 24, 2019 10:35
Show Gist options
  • Select an option

  • Save grim-reapper/a90eff2a8db046fffbb2e3f5c4bb802b to your computer and use it in GitHub Desktop.

Select an option

Save grim-reapper/a90eff2a8db046fffbb2e3f5c4bb802b 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment