Skip to content

Instantly share code, notes, and snippets.

@mahedi2014
Created April 2, 2016 04:30
Show Gist options
  • Select an option

  • Save mahedi2014/01051c37571ac1a7ccdc63f2a09aae36 to your computer and use it in GitHub Desktop.

Select an option

Save mahedi2014/01051c37571ac1a7ccdc63f2a09aae36 to your computer and use it in GitHub Desktop.
js print a div html
<!DOCTYPE html>
<html>
<head>
<script>
function printContent(el){
var restorepage = document.body.innerHTML;
var printcontent = document.getElementById(el).innerHTML;
document.body.innerHTML = printcontent;
window.print();
document.body.innerHTML = restorepage;
}
</script>
</head>
<body>
<h1>My page</h1>
<div id="div1">DIV 1 content...</div>
<button onclick="printContent('div1')">Print Content</button>
<div id="div2">DIV 2 content...</div>
<button onclick="printContent('div2')">Print Content</button>
<p id="p1">Paragraph 1 content...</p>
<button onclick="printContent('p1')">Print Content</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment