Print div using Javascript


Here is a full code to print contents of a div using javascript. Just create a div and give any id to it and then create an anchor tag as given below :


<a href="javascript:void(0);" onclick="printdiv('IDOFDIV')">Print</a>

After creating this anchor tag put the below javascript code in your page.


function printdiv(printpage) {

var newstr = document.getElementById(printpage).innerHTML;

var oldstr = document.body.innerHTML;

document.body.innerHTML =newstr;

window.print();

document.body.innerHTML = oldstr;

return false;

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s