Sometimes, we want to print part of a rendered HTML page in JavaScript.
In this article, we’ll look at how to print part of a rendered HTML page in JavaScript.
How to print part of a rendered HTML page in JavaScript?
To print part of a rendered HTML page in JavaScript, we can open a new window with the element we want to print.
For instance, we write
const newWindow = window.open();
newWindow.document.write(document.getElementById("output").innerHTML);
newWindow.print();
to call window.open
to open a new window.
Then we call newWindow.document.write
to render the HTML string of the element we want to print.
We get the element with getElementById
and get the string with the HTML code for the element with innerHTML
.
Next we call print
to open the print dialog to print the stuff in the new window.
Conclusion
To print part of a rendered HTML page in JavaScript, we can open a new window with the element we want to print.