jquery - javascript to print a specific div -


i got code site print div:

    function printdata() {    var divtoprint=document.getelementbyid("printtable");    newwin= window.open("");    newwin.document.write(divtoprint.outerhtml);    newwin.print();     newwin.close(); }  $('button').on('click',function(){ printdata(); }) 

how add css code javascript apply table id='table' ?

table, td, th {     border: 1px solid black;     text-align:justify; }  th {     background-color: #7a7878;     text-align:center } 

please have at: http://jsfiddle.net/rhjv1u11/

 function printdata() {    var divtoprint=document.getelementbyid("printtable");    newwin= window.open("");    newwin.document.write(divtoprint.outerhtml);     var css =`table, td, th {         border: 1px solid black;         text-align:justify;     }      th {         background-color: #7a7878;         text-align:center     }`;    var div = $("<div />", {     html: '&shy;<style>' + css + '</style>'   }).appendto( newwin.document.body);    newwin.print();     newwin.close(); }  $('button').on('click',function(){ printdata(); }); 

the example inserts style element conatining rules. if css need assign little example suggests (and not need lot of maintenance), sufficient solution.

in case more, complex css should access newwin.document.head instead , insert node load external css. somehow this:

var linkelement=document.createelement("link");  linkelement.setattribute("rel", "stylesheet"); linkelement.setattribute("type", "text/css"); linkelement.setattribute("href", "your.css.file.here.css") newwin.document.getelementsbytagname("head")[0].appendchild(linkelement) 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -