Skip to content

Instantly share code, notes, and snippets.

@iansltx
Created February 3, 2015 08:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iansltx/2905c9336e369fb7e6c1 to your computer and use it in GitHub Desktop.
Save iansltx/2905c9336e369fb7e6c1 to your computer and use it in GitHub Desktop.
Force element re-render on print
(function() { // force re-render of element_name to get the styling right on-print
var beforePrint = function() {
var el = document.getElementById('element_name'); // e.g. an input inside a <p>...
var p = el.parentNode; // ...which will normally fail to style entirely on-print...
p.removeChild(el); // ...but if you wrap it in a <span> or the like, then remove and re-add the element...
p.appendChild(el); // ...the styles will recalculate and you'll get whatever style you were trying to apply!
};
if (window.matchMedia) {
window.matchMedia('print').addListener(function(mql) {
if (mql.matches) beforePrint();
});
}
window.onbeforeprint = beforePrint;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment