Print selected webpage data using Javascript

Many times we would like to place a button on webpage to print the content of that web page via an actual printer. JavaScript helps you to implement this functionality using the print function of window object.

The JavaScript print function window.print() prints the current web page when executed. You can call this function directly using the onclick event and also set which portion would be printed.

See the Example as below :

<html xmlns="">
        <script type="text/javascript">
            function printthis() {
                var printthis = document.getElementById('printthis');
                var popupWin ='', '_blank', 'width=1000,height=600');
                popupWin.document.write('<html><body onload="window.print()">' + printthis.innerHTML + '</html>');
            Other contents
        <div id="printthis">
                Print this portion
            <input type="button" value="print" onclick="printthis();" />


sometimes we want some portion of the page need to be print not a whole page,as per above example set id or class to portion which need to print (here #printthis).
when javascript function call on onclick then this function prints part of page which having data within #printthis div.

Leave a Comment