I use jQuery for this but I can’t use it now (no reason for this). I need to select a portion of a table and then print it.
I use this code:
function Printon(this)
{
newWin= window.open('','','toolbar=yes,location=no,directories=yes,menubar=yes,scrollbars=yes,width=800, height=1000, left=10, top=25');
newWin.document.write('<table>' + document.getElementsByName(this).innerHTML + '</table>');
newWin.document.close();
newWin.focus();
newWin.print();
newWin.close();
return false;
}
When then calling the function with parameter of the <tr> names I just get a document with:
12-11-12
undefined
I am sure my HTML is correct but still here is my HTML:
<tr name="Print_1">
<td>dhr Kees-jan ckc Von fleppenstein</td>
<td>aquaduct straat 66<br>0606 OP ORK</td>
<td>WDB-1352303696</td>
<td>2822.00</td>
<td><input type="button" value="Print this" onclick="Printon('Print_1')"></td>
<td> </td>
</tr>
<tr name="Print_1">
<td>-</td>
<td> </td>
<td> </td>
<td>46.00</td>
<td>2</td>
<td>Actiesalade herfst</td>
</tr>
<tr name="Print_1">
<td>-</td>
<td> </td>
<td> </td>
<td>46.00</td>
<td>2</td>
<td>Actiesalade herfst</td>
</tr>
On inspiration of @Muthu Kumaran I use this:
function Printon(dit)
{
var arr = new Array();
arr = document.getElementsByName(dit);
var tabel = '';
for(var i = 0; i < arr.length; i++){
tabel += document.getElementsByName(dit)[i].outerHTML;
}
newWin= window.open('','','toolbar=yes,location=no,directories=yes,menubar=yes,scrollbars=yes,width=800, height=1000, left=10, top=25');
newWin.document.write('<table border=\"2\" cellpadding=\"2\" cellspacing=\"0\">' + tabel + '</table>');
newWin.document.close();
newWin.focus();
newWin.print();
newWin.close();
return false;
}
Fixed it works now!
Don’t use
thisas a argument in the function. Give a proper variable like name. Alsodocument.getElementsByNamewill return array of elements, so you have to get the value by usingdocument.getElementsByName(name)[0]Try this,
Working Demo: http://jsfiddle.net/muthkum/RxNKn/1/