JSFiddle - React, Tailwind, and code Playground

by Per Andersson

HTML

<div>Hej</div>

JavaScript

function printBody() {
      // store oiginal content
      var originalContents = document.body.innerHTML;

      // get the outer html of the document element
      document.body.innerText = document.documentElement.outerHTML;

      // call window.print if you want it on paper
      window.print();

      // or put it into an iframe
      // var ifr = document.createElement('iframe');
      // ifr.src = 'data:text/plain;charset=utf-8,' + encodeURI(document.documentElement.outerHTML);
      // document.body.appendChild(iframe);
    
      // a small delay is needed so window.print does not get the original
      setTimeout(function(){
        document.body.innerHTML = originalContents;
      }, 2000);
    }
    
    printBody();