0
<html>
<head>
<script type="text/javascript">

    function PrintElem()
    {
            var mydiv1 = document.getElementById("div1");
            var mydiv2= mydiv.getElementsByTagName("div2");
       printTheDivs(mydiv1, mydiv2);
    }

    function printTheDivs (d1,d2) 
    {
        var mywindow = window.open('', 'my div', 'height=400,width=600');
         mywindow.document.write('<html><body>' + d1.innerHTML + '</body></html>');
//Here I want to show the Images in this window.

$(d2).print();

//but want to print the Div2 Images I’m using the jquery.print.js plugin but not working. It is printing complete page. How to accomplish the task with multiple Browsers.


    }

</script>
</head>
<body>

<div id="div1">
<img src=”image1.jpg”/>
<img src=”image2.jpg”/>
<img src=”image3.jpg”/>
<img src=”image4.jpg”/>
<img src=”image5.jpg”/>
</div>

<div id="div2">
<img src=”image6.jpg”/>
<img src=”image7.jpg”/>
<img src=”image8.jpg”/>
<img src=”image9.jpg”/>
<img src=”image10.jpg”/>
</div>

<input type="button" value="Print Div" onclick="PrintElem()" />

</body>
</html>

I want to show one div using window.open() and print another div. I’m using the jquery.print.js plugin but not working. It is printing complete page. How to accomplish the task with multiple Browsers. Please help me. Thanks in advance.

Billy
  • 825
  • 6
  • 21
  • 36

1 Answers1

0

You can print particular div

<div id="divid">test</div>

function printDiv(divID) {
        //Get the HTML of div
        var divElements = document.getElementById(divID).innerHTML;
        //Get the HTML of whole page
        var oldPage = document.body.innerHTML;

        //Reset the page's HTML with div's HTML only
        document.body.innerHTML = 
          "<html><head><title></title></head><body>" + 
          divElements + "</body>";

        //Print Page
        window.print();

        //Restore orignal HTML
        document.body.innerHTML = oldPage;


    }
    printDiv('divid')

check here http://jsfiddle.net/jrhp8/

Asif
  • 647
  • 9
  • 18
  • but this causing the window(Window.Open()) to get loading when replacing the body content. Also Page is not functioning properly after replacing the body content until I refresh the page. Is there any other solution? – Billy Nov 01 '13 at 04:36