Print the contents of a DIV
Let's get straight to the point. Want to print a DIV's content? Trigger a popup with that content, and run your browser's print function. Here's the quick, ready-to-deploy code:
Get this printer rolling by calling printDiv('yourDivId')
, where 'yourDivId'
is the ID of your go-to DIV. The script will handle the rest — loading the content and hitting the print buttons for you.
Advanced problems? Advanced solutions!
The strange case of dynamic content
Is your DIV filled with forms or fancy JavaScript pieces? You'll need to approach it differently. Clone the DOM subtree, voilà! You preserve the interactivity before you hit print:
Fashion matters: Styling for print
Looks matter. Sometimes, printing might not Instagram your beautiful CSS styles. Use @media print to shower your print with styles, or inline some styles when needed:
Extra! Extra! Images and iframes
You've got images or iframes? Ensure they're all loaded and ready for their closeup before you print:
Browsers, always the divas!
Cross-browser compatibility
Test your printing function across different browsers. Different browser, different dance rules. Keep an eye out for fallbacks for the old folk.
Chrome-specific considerations
Chrome, the rockstar. Make sure you're not blocked by any popup-blocking features. Rockstars hate party poopers.
Printing forms, not just papers
Does your DIV host forms? Make sure those values make it to the print. Run through each form input, and trash "CTRL+C" with this bad boy:
Was this article helpful?