Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
How to make 2 buttons to print 2 different parts in web page?
Posted

1 solution

Save below code in html file and test..

Its Work perfect!!

XML
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="_Default" %>

<html>
<head>
    <style type="text/css">
        #dv1, #dv2
        {
            font-size: 20px;
        }
    </style>
    <style media="print">
        .hidePrint
        {
            display: none;
        }
    </style>
    <script type="text/javascript" language="javascript">
        function fnSetStyle(Obj) {
            if (Obj == 1) {
                document.getElementById('dv1').className = '';
                document.getElementById('dv2').className = 'hidePrint';
            }
            else {
                document.getElementById('dv2').className = '';
                document.getElementById('dv1').className = 'hidePrint';
            }
        }
    </script>
    <title></title>
</head>
<body>
    <div id="dv1">
Portion 1: Place one portion here
    </div>
    <div id="dv2">
Portion 2: and another here..
    </div>
Common portion: and click on Div1 to print first portion and click on Div2 to print another portion<br />
    <input type="button" value="Div1" onclick="javascript:fnSetStyle(1);window.print();" />
    <input type="button" value="Div2" onclick="javascript:fnSetStyle(2);window.print();" />
</body>
</html>
 
Share this answer
 
v2
Comments
Eman Ayad 6-Nov-12 0:29am    
Thank you but it doesn't work,
This example has worked, the problem was with the double quotations of the onclick function when embed it in php code even when use single quote,
Now when remove it totally it has worked.


<html>
<head>
<script type='text/javascript' language='JavaScript'>
function printPage(id)
{
var html="<html>";
html+= document.getElementById(id).innerHTML;
html+="</html>";

var printWin = window.open('','','left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status =0');
printWin.document.write(html);
printWin.document.close();
printWin.focus();
printWin.print();
printWin.close();
}
</script>
</head>
<body>
<div id="header">Header Content</div>
<div id="content">actual content</div>
<div id="footer">Footer content</div>
<input type="button" value="Printcontent" önclick="printPage('content');"></input>

<input type="button" value="Print1" önclick="printPage('area1');"></input>
<div id="area1">Area1 content</div>

<div id="area2">Area2 content</div>
<input type="button" value="Print2" önclick="printPage('area2');"></input>
</body>
</html>
Kaushikhalvadiya 9-Nov-12 9:49am    
man its working perfect!!

now you've changed your requirements!!

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