I am trying to implement a print feature in HTML. I know I can print the whole page with window.print()
, but how do I print only a specific page element? For ex
Simple html and pure javascript works best. Parameter "this" refers to current id, so that function is universal for all ids with textual content.
html body:
text i want to print
pure javascript:
//or: monitor.textContent = "click me to print textual content";
const idElementPrint = (idin) => {
let ifram = document.createElement("iframe");
ifram.style = "display:none";
document.body.appendChild(ifram);
pri = ifram.contentWindow;
pri.document.open();
pri.document.write(idin.textContent);
pri.document.close();
pri.focus();
pri.print();
}