I wrote a tiny JavaScript module called PrintElements to dynamically print parts of a web page.
It works by iterating over the selected elements of the node and for each node iterates through the DOM tree to the BODY element. At each level, including the start (which is the node level for printing), it attaches the peer class ( pe-preserve-print ) to the current node. Then it attaches another marker class ( pe-no-print ) to all the siblings of the current node, but only if they do not have the pe-preserve-print class. As a third act, it also attaches another class to the stored elements of the ancestor pe-preserve-ancestor .
A simple simple additional print-only css will hide and show the relevant elements. Some of the advantages of this approach are that all styles are saved, it does not open a new window, there is no need to move many DOM elements, and, as a rule, it is non-invasive with your original document.
See the demo , or read the related article for more information .
András Szepesházi Mar 11 '19 at 9:17 2019-03-11 09:17
source share