How to print div contents keeping CSS

15,378

Solution 1

How about

function printDiv(id) {

  var html = "";

  $('link').each(function() { // find all <link tags that have
    if ($(this).attr('rel').indexOf('stylesheet') !=-1) { // rel="stylesheet"
      html += '<link rel="stylesheet" href="'+$(this).attr("href")+'" />';
    }
  });
  html += '<body onload="window.focus(); window.print()">+$("#"+id).html()+'</body>';
  var w = window.open("","print");
  if (w) { w.document.write(html); w.document.close() }

}

Solution 2

https://github.com/jasonday/printThis

Plugin I authored for this exact scenario.

Share:
15,378
mukamaivan
Author by

mukamaivan

Updated on June 05, 2022

Comments

  • mukamaivan
    mukamaivan almost 2 years

    I would like to print div contents while keeping the css applied on the div and it inner html.

    Am using jQuery Print Element but its striping off the css.