Skip to content

Instantly share code, notes, and snippets.

@karthiks
Forked from andrewlimaza/example.html
Last active May 17, 2020 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karthiks/62eff684c1d9f3e9cd8ef758a4973f42 to your computer and use it in GitHub Desktop.
Save karthiks/62eff684c1d9f3e9cd8ef758a4973f42 to your computer and use it in GitHub Desktop.
Print certain div / elements using window.print()
<script>
function printDiv(divID){
var printContents = document.getElementById(divID).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