Print the Content of a Div Element

Print the Content of a div Element using HTML & JavaScript

Print the Content of a div Element using HTML & JavaScript

Hey Guys, In today’s blog, we are going to see how to create and print the content of a div element using HTML and JavaScript. We use HTML and Javascript code to print the content of a div element.

Here is a preview of how we print the content of a div.

Print the Content of a div Element using HTML & JavaScript

This is a very simple project in which the content will be contained in a div container that will be created using a basic HTML element, and using the button command, the content will be printed inside the div area. This project teaches you how to construct a print button that lets users print any webpage they need while also teaching you the fundamentals of printing.

50+ HTML, CSS and JavaScript Projects With Source Code

Now The Project is going to create and for that, we are first adding an HTML Code.

HTML Code:

<div id="printableArea">
      <h1>Print me</h1>

<input type="button" onclick="printDiv('printableArea')" value="print a div!" />

First We create a div class with the name “printableArea” and Inside the div class we add a header using an H1 tag with the content Print me, Then We close the Div tag.

Portfolio Website Using HTML ,CSS and Javascript Source Code

Then We create an input class as button type and set the onclick function by giving a name for it and finally a value “print a div ” is added.

So that’s for HTML, Now We directly go for JavaScript to print the div area. Here we skip the CSS, if you want means you can use it. As it is a sample of the project so we directly added the content.

HTML Output:

Print the Content of a div Element using HTML & JavaScript

JavaScript Code:

function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;


     document.body.innerHTML = originalContents;


Here first, we create a function and call the onclick function, which is in HTML, to make the contents inside work. Now we are creating two variables, namely “printContents” and “originalContents,” one for calling out the element using the JS get Element property and one for printing the contents inside of the div class.

After that, we are storing the printContents values to the originalContents value.

Create Resume/CV Website Using HTML and CSS (Source Code)

Then we use the JS property window. print to make a print of the contents inside the div class.

Lastly, the contents, which are inside the variable “originalContents,” were again stored in the same variable declared in the three lines above.

So that’s for JS. Now we have come to the end of our project as we have successfully added the source codes. So we can now move on to the project preview, which is mentioned below in the output section.

Video Output Of Print the Content of a div Element:

Final Output Of Print the Content of a div Element:

Now We have Successfully created Print the Content of a div Element Using Html and JavaScript. You can use this project for your personnel needs and the respective lines of code are given with the code pen link mentioned below.

100+ JavaScript Projects With Source Code ( Beginners to Advanced)

If you find out this Blog helpful, then make sure to search Codewithrandom on Google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

REFER CODE- Greg Vissing


WRITTEN BY- Ragunathan


What is the use of Div tag?

To create a division or section, use the empty container known as the <div> tag. It is used to group HTML components so they can be styled with CSS or manipulated with scripts without changing the content or layout.


How to Create a Print button?

In order to make a print button, we first need to create a button using an HTML button element. Next, using the javascript function window print (), we add the print function to our on-click button event listener.


Leave a Reply