Skip to content

Instantly share code, notes, and snippets.

@Dilshan97
Last active June 30, 2019 11:22
Show Gist options
  • Save Dilshan97/c83aa0a91ec89260799395144fc7a2b1 to your computer and use it in GitHub Desktop.
Save Dilshan97/c83aa0a91ec89260799395144fc7a2b1 to your computer and use it in GitHub Desktop.
Print a specific div in html
<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