How to Print from JavaScript HTML using window.print

Ever came across a need where you want to print the HTML form or page. This is possible using the JavaScript window.print() function. You can use this code with any control (like Button) and even any event (like onclick). This will simply bring up the printer options from your machine and then you can follow the normal procedure of printing.

Note - A printer must be configured on your machine.


This will not directly print the page, similar to the way you do it on your machine. The printer options window will show and you can make adjustments and settings before actually printing the contents.

Try Out Code2care Dev Tools:


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.