DEV Community

Codewithrandom Blogs
Codewithrandom Blogs

Posted on

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 For Print the Content of a Div Element.

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.

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>
</div>

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

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.

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.

JavaScript Code:

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

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}
Enter fullscreen mode Exit fullscreen mode

Here first we create an function and calling out the onclick function which is in HTML to make contents inside to work it. Now We creating two variables namely "printContents" and "originalContents" one for calling out the element using JS get Element property and one for printing the contents inside of div class.

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

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

Lastly the contents which is inside of variable "originalContents" were again stored to the same variable declared three lines above.

So that's for JS , Now We came to end of our Project as we successfully added the source codes. So we can now move for project preview which is mentioned below of Output Section.

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.

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

Top comments (0)