How to print div content using javascript?

Today, We want to share with you print div in javascript.In this post we will show you To print the data content of div in js, added content of div in a js variable after that the print button is clicked, hear for Print the content of a div element using JavaScript we will give you demo and example for implement.In this post, we will learn about jQuery Print Dynamic Content using PHP MySQL with an example.

How to print div content using javascript/jQuery?

Example 1: index.html

<!DOCTYPE html>
<html lang="en">
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
<link rel="stylesheet" href="">
   <div id='writeNameOnPictureDownload'>
      <p>I am inside the div tag...</p>
   <p>I am not inside the div tag...</p>
   <input type='button' id='getDataPage' value='Press Me To display the data'>
   <div id="pageDataDisplay"></div>
   $('#getDataPage').on('click', function () {
   function showContent() {
      $(document).ready(function () {

JS: Print content of div


<h2>JS: Print content of div</h2>

<div class="hidden">
  <div id="pageDataDisplay">
    <h3>This will be printed</h3>

  <h3>This won't be printed</h3>

<button id="writeNameOnPictureDownload">Print</button>

CSS Code

.hidden {

body {
  background: #edfffe;

JS Code

document.getElementById("writeNameOnPictureDownload").addEventListener("click", function() {
     var printContents = document.getElementById('pageDataDisplay').innerHTML;
     var originalContents = document.body.innerHTML;
     document.body.innerHTML = printContents;
     document.body.innerHTML = originalContents;

I hope you get an idea about div content with background image javascript.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.