Friday, January 13, 2017

Print div in javascript with css

You need to change the css property media to print. Add new line to your function createPopup() as below you attached your css:
mywindow.document.write( "<link rel=\"stylesheet\" href=\
"style.css\" type=\"text/css\" media=\"print\"/>" );


function createPopup( data ) {
var mywindow = window.open( "", "new
 div", "height=400,width=600" );
mywindow
.document.write( "<html><head>
<title></title>" );
mywindow
.document.write( "<link rel=\
"stylesheet\" href=\"style.css\
" type=\"text/css\"/>" );
mywindow
.document.write( "</head><body >" );
mywindow
.document.write( data );
mywindow
.document.write( "</body></html>" );

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

return true;

}
document
.addEventListener( "DOMContentLoaded", 
 function() {
document
.getElementById( "print" )
.addEventListener( "click", function() {
createPopup
( document
 .getElementById( "content" ).innerHTML );

}, false );

});

})();

No comments:

Post a Comment