Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have got project that I am working on that requires a print function. This print function basically just prints a form with all the data that is filled up. I am aware of the
HTML
onclick="window.print()
but this function prints out the entire page , more like a print print preview.

Is there any way I can focus and print the form in html/javascript? or is possible to specify and just print code elements that are contained in a div tag??



XML
<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>

  <body>

  <div> //can some how specify to only print elements in this div??
    <label> First Name:</label>
    <input type="text"> <br>
    <label> Last Name: </label>
    <input type="text">
     </div> // up to here...

    <button onclick="window.print()" > Print </button> //onlick here prints the whole page, which is not what i am looking for.

  </body>


</html>



Thank you for your help in advance.
Posted

Refer - Printing content of a particular div[^].

Logic is to open that div in another window and then print. That's it.
 
Share this answer
 
You can achieve this using the jquery Library

jquery Library

and

Jquery Plugin

Write this....

ASP.NET
<head>
    <title>Invoice</title>
    <script src="AdminPanel/js/jquery-1.11.1.min.js" type="text/javascript"></script>
    <script src="js/jquery.PrintArea.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#print_button").click(function () {
                $(".PrintArea").printArea();
            });
        });
     </script>
</head>
<body>
    <form id="form1"  runat="server">

    
	<div class="PrintArea">
				
	
	</div>		
	
	
</form></body>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900