Question : Printing multiple divs using java script

Hi,
Please refer -  http://www.experts-exchange.com/Programming/Languages/Scripting/JScript/Q_24517798.html?sfQueryTermInfo=1+10+30+div+java+print+script

The solution is fine, but how do we print multiple divs, either with same ID or varying IDs

I got another similar code as copied below, which I calling through: <a href="javascript:void(processPrint());">Print</a>    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
<script language="javascript">

var gAutoPrint = true;

function processPrint(){

if (document.getElementById != null){

var html = '<HTML>\n<HEAD>\n';

if (document.getElementsByTagName != null){

var headTags = document.getElementsByTagName("head");

if (headTags.length > 0) html += headTags[0].innerHTML;

}

html += '\n</HE' + 'AD>\n<BODY>\n';

var printReadyElem = document.getElementById("printMe");

if (printReadyElem != null) html += printReadyElem.innerHTML;

else{

alert("Error, no contents.");

return;

}

html += '\n</BO' + 'DY>\n</HT' + 'ML>';

var printWin = window.open("","processPrint");

printWin.document.open();

printWin.document.write(html);

printWin.document.close();

if (gAutoPrint) printWin.print();

} else alert("Browser not supported.");

}

</script>

Answer : Printing multiple divs using java script

Missing bracket and other things.

Here is a better version
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
<html>
   <head>
 
<script language="javascript">

var gAutoPrint = true;

function processPrint() {
  if (document.getElementById != null) {
    var html = '<HTML>\n<HEAD>\n';
    if (document.getElementsByTagName != null) {
      var headTags = document.getElementsByTagName("head");
      if (headTags.length > 0) html += headTags[0].innerHTML;
    }
    html += '\n</HE' + 'AD>\n<BODY '+((gAutoPrint)?'onLoad="window.print()"':'')+'>\n';
    var elems = document.getElementsByTagName("div");
    var divContent = "";
    for (var i=0;i<elems.length;i++) {
      if (elems[i].className=="printMe") divContent += elems[i].innerHTML+'<br />';
    }
    if (divContent!="") {
      html += divContent;
      html += '\n</BO' + 'DY>\n</HT' + 'ML>';
      var printWin = window.open("","processPrint");
      printWin.document.write(html);
      printWin.document.close();
    }
    else alert("Error, no contents.");
    return false;
  }
}

</script>


      
      
   </head>
   <body>
    
      <div class="printMe" > This is the content for printMe </div>
       <div class="printMe" > This is the other content for printMe on the same page </div>
<a href="#" onClick="return processPrint();">Print</a> 
   </body>
</html>
Random Solutions  
 
programming4us programming4us