Print Div Content from a page using Javascript

Here is my aspx page where I have two div of which i want to print 1st div 
Here is the trick with small javascript 

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="PrintDivContent.aspx.vb"
    Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
    <title>Untitled Page</title>

    <script language="javascript" type="text/javascript">
        function printDiv(divID) {
            //Get the HTML of div
            var divElements = document.getElementById(divID).innerHTML;
            //Get the HTML of whole page
            var oldPage = document.body.innerHTML;

            //Reset the page's HTML with div's HTML only
            document.body.innerHTML = "<html><head><title></title></head><body>" + divElements + "</body>";

            //Print Page

            //Restore orignal HTML
            document.body.innerHTML = oldPage;

            //disable postback on print button
            return false;

    <form id="form1" runat="server">
    <div id="printablediv" style="width: 100%; background-color: Blue; height: 200px">
        Print me I am in 1st Div
    <div id="donotprintdiv" style="width: 100%; background-color: Gray; height: 200px">
        I am not going to print
    <input type="button" value="Print 1st Div" onclick="javascript:printDiv('printablediv')" />

Please Reply if you have any query

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now