View Single Post
Old 11-29-2012, 01:53 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
This is a mildly hacky way to do what you want without mucking with a separate window.

Code:
<html>
<head>
<style type="text/css">
@media print {
    .noprint { display: none; }
    .printme { display: block; }
}

#funk {
    width: 50%;
    border: solid red 3px;
}
.isblue { 
    border: solid blue 5px;
    background-color: lightblue;
}
#zonk {
    width: 40%;
}
</style>
<script type="text/javascript">
function printDiv( byId )
{
    // this loop could use getElementByClassName if not using MSIE8 and below
    var divall = document.getElementsByTagName("div");
    for ( var d = 0; d < divall.length; ++d )
    {
        var div = divall[d];
        div.className = div.className.replace("printme","noprint");
    }
    // now, turn "on" only one div for printing:
    var div = document.getElementById( byId );
    div.className = div.className.replace("noprint","printme");
    window.print( );
}
</script>
</head>
<body>
<div id="funk" class="noprint">
   <h1>Yowser!</h1><br/>
   Want to print me?<br/>
   <input type="button" value="Then click here!" onclick="printDiv('funk');"/>
</div>
<div class="noprint">
    Stuff that will never get printed!
</div>
<div id="zonk" class="isblue noprint">
   <h2>Coupon worth $2 million dollars!<h2>
   <br/>
   <i>Expires April 1, 1932</i>
   <br/>
</div>
<a class="noprint" href="#" onclick="printDiv('zonk'); return false;">PRINT $2,000,000 COUPON</a>

</body>
</html>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 11-29-2012 at 01:57 AM..
Old Pedant is offline   Reply With Quote