You are currently viewing Print the Content of a Div Element using HTML & JavaScript

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 an Print the Content of a Div Element. We Use Html and Javascript Code For Print the Content of a Div Element. For that, the respective source codes were given below along with the respective preview of the output section. So you can make use of that.

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!" />

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.

 

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;
}

 

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.

FINAL OUTPUT Of Print the Content of a Div Element:


Now We have Successfully created our Simple Div Print Using HTML & JAVA SCRIPT. 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 code with random 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

Leave a Reply