Friday, 20 March 2015

Print Selected content on HTML Page

Here is the code for printing selected content on HTML Page.

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" language="javascript">
        function PrintDivContent() {
            var divContents = document.getElementById("dvContainer").innerHTML;
            var printWindow = window.open('', '', 'height=400,width=800');
            printWindow.document.write('<html><head><title>DIV Contents</title>');
            printWindow.document.write('</head><body >');
            printWindow.document.write(divContents);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();
        }
     </script>
</head>
<body>
    <div id="dvContainer">
        This content needs to be printed.
    </div>
    <input type="button" value="Print Div Contents" id="btnPrint" onclick="PrintDivContent();" />
</body>
</html>

--
Happy Coding
Gopinath

No comments:

Post a Comment