Thursday, October 8, 2015

How to print a div

You can print any div by using this code. Just copy and pest this code in your html page what I given below:
<html>
<head> 
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </script> 
<!-- You should use JavaScript--> 
<script type="text/javascript">

    function PrintElem(elem)
    {
        Popup($(elem).html());
    }

    function Popup(data) 
    {
        var mywindow = window.open('', 'my div', 'height=400,width=600');
        mywindow.document.write('<html><head><title>my div</title>');
        /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
        mywindow.document.write('</head><body >');
        mywindow.document.write(data);
        mywindow.document.write('</body></html>');

        mywindow.document.close(); // necessary for IE >= 10
        mywindow.focus(); // necessary for IE >= 10

        mywindow.print();
        mywindow.close();

        return true;}
</script>
</head>
<body>
<div id="mydiv"> <!-- This is the div what you want to print -->
    Hello world. I want to printed by your pronter. So print me now. 
Hello world. I want to printed by your pronter. So print me now. 
Hello world. I want to printed by your pronter. So print me now. 
   </div>
<div>
    This will not be printed.
</div>
<div id="anotherdiv">
    Nor will this.
</div>
<input type="button" value="Print Div" onclick="PrintElem('#mydiv')" />

</body>
</html>
 
Just try your self. Thank you 

0 comments:

Post a Comment