c5xzpw
Last Updated: February 25, 2016
·
47.8K
· emincan

Printing the Contents of a Div with Javascript

A clean way to print the contents of a container with javascript:

function printElem(divId) {
    var content = document.getElementById(divId).innerHTML;
    var mywindow = window.open('', 'Print', 'height=600,width=800');

    mywindow.document.write('<html><head><title>Print</title>');
    mywindow.document.write('</head><body >');
    mywindow.document.write(content);
    mywindow.document.write('</body></html>');

    mywindow.document.close();
    mywindow.focus()
    mywindow.print();
    mywindow.close();
    return true;
}

Source: http://stackoverflow.com/questions/2255291/print-the-contents-of-a-div