Print particular section in Javascript
function printDiv(divName) {
var date = divName.parentElement.parentElement.querySelectorAll("td")[0].innerHTML;
var senderNameNumber = divName.parentElement.parentElement.querySelectorAll("td")[1].innerHTML;
var accountNumber = divName.parentElement.parentElement.querySelectorAll("td")[2].innerHTML;
var AccountHolderName = divName.parentElement.parentElement.querySelectorAll("td")[3].innerHTML;
var Amount = divName.parentElement.parentElement.querySelectorAll("td")[4].innerHTML;
var Tax = divName.parentElement.parentElement.querySelectorAll("td")[5].innerHTML;
var status = divName.parentElement.parentElement.querySelectorAll("td")[6].innerText;
var data = `<table class='table table-bordered'>
<tr>
<th>Sender Name & Number </th>
<td>${senderNameNumber}</td>
</tr>
<tr>
<th>Account Holder Name</th>
<td>${AccountHolderName}</td>
</tr>
<tr>
<th>Account Number</th>
<td>${accountNumber}</td>
</tr>
<tr>
<th>Amount</th>
<td>${Amount}</td>
</tr>
<tr>
<th>Tax</th>
<td>${Tax}</td>
</tr>
<tr>
<th>Status</th>
<td>${status}</td>
</tr>
<tr>
<th>Date & Time</th>
<td>${date}</td>
</tr>
</table>`;
var title = document.title;
var divElements = data;
var printWindow = window.open("", "_blank", "");
printWindow.document.open();
//write the html to the new window, link to css file
printWindow.document.write('<html><head><title>' + title + '</title><link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"></head><body>');
printWindow.document.write(divElements);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.focus();
//The Timeout is ONLY to make Safari work, but it still works with FF, IE & Chrome.
setTimeout(function() {
printWindow.print();
printWindow.close();
}, 100);
}
Comments
Post a Comment