Print div contents using JavaScript

Mar 6, 2015 By Mohammed Imtiyaz

Introduction

This tutorial will explain you how to print div contents on web page (Asp.net, html, etc) using JavaScript

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Print.aspx.cs" Inherits="Print" %>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

   

    <title>Print div contents using JavaScript</title>

   

    <script language="javascript" type="text/javascript">

        function Print(el) {

            var printContent = document.getElementById(el).innerHTML;

            printWindow = window.open("", "myWindow", "top=120, left=200, width=550, height=400, dependant=no, location=0, alwaysRaised=no, menubar=no, resizeable=no, scrollbars=yes, toolbar=no, status=no, center=yes");

            printWindow.document.write("<div>");

            printWindow.document.write("" + printContent + "");

            printWindow.document.write("</div>");

            printWindow.document.close();

            printWindow.print();

            printWindow.close();

        }

    </script>

 

</head>

<body>

    <form id="form1" runat="server">

    <div id="div1" style="width: 550px">

        Div Contents Here...

    </div><br />

    <input id="Button1" type="button" value="Print" onclick="Print('div1')" />

    </form>

</body>

</html>



Post your comment