Skip to content

Instantly share code, notes, and snippets.

@Komock
Created May 15, 2020 14:05
Show Gist options
  • Save Komock/a71a6d6bbebc52e55b01d9f56163e4b5 to your computer and use it in GitHub Desktop.
Save Komock/a71a6d6bbebc52e55b01d9f56163e4b5 to your computer and use it in GitHub Desktop.
Print HTML element by selector
function printElem(selector, width, height) {
const newWindow = window.open('', 'PRINT', `height=${height},width=${width}`);
const element = document.querySelector(selector);
newWindow.document.write(`
<html>
<head>
<title>${document.title}</title>
</head>
<body>${element.innerHTML}</body>
</html>`);
newWindow.document.close(); // necessary for IE >= 10
newWindow.focus(); // necessary for IE >= 10*/
newWindow.print();
newWindow.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment