Print html via javascript / jquery

I want to print a document by passing custome html code.

non-working code that I wrote:

function Clickheretoprint()
{
  var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
      disp_setting+="scrollbars=yes,width=780, height=780, left=100, top=25"; 
  var content_vlue = document.getElementById("result_tbl").innerHTML; 

  var docprint=window.open("","",disp_setting); 
  docprint.document.open(); 
  docprint.document.write('<html><head><title>Ashley Mattresses</title>'); 
  docprint.document.write('</head><body onLoad="self.print()"><center>');          
  docprint.document.write('<TABLE width="100%" cellpadding=10 align=center  valign="top"><TR valign="top"><TD width = "33%" valign="top">col1</TD><TD width = "33%" valign="top">col2</TD><TD width = "33%" valign="top">col3</TD></TR></TABLE>');     
  docprint.document.write('</center></body></html>'); 
  docprint.document.close(); 
  docprint.focus(); 
  docprint.close();
}

This is the method that I called in the href of the attached tag, but did not do the work:

<a href="javascript:Clickheretoprint()">

I'm new to javascript / jQuery encoding, please help me achieve the same (by fixing this or suggesting an approach) as an urgent requirement.

Thanks in advance.

+1
source share
3 answers

You are on the right track. Assuming that result_tblis the identifier of the element that you want to print as is, first wrap the element with a tag <div>, for example:

<div>
    <table id="result_tbl">
        .....
    </table>
</div>

Then we get the following code:

var docprint=window.open("about:blank", "_blank", disp_setting); 
var oTable = document.getElementById("result_tbl");
docprint.document.open(); 
docprint.document.write('<html><head><title>Ashley Mattresses</title>'); 
docprint.document.write('</head><body><center>');
docprint.document.write(oTable.parentNode.innerHTML);
docprint.document.write('</center></body></html>'); 
docprint.document.close(); 
docprint.print();
docprint.close();

Live test .

Chrome 18:

print is working

+7

: http://jsfiddle.net/5Wfqr/3/

:

var content_vlue = document.getElementById("result_tbl").innerHTML; 

: "result_tbl" HTML?

+1

This code works great for me

<body>
        <script type="text/javascript">
            function printPage() {

                var docprint = window.open("about:blank", "_blank"`enter code here`, "channelmode");    
                var oTable = document.getElementById("tbl");
                docprint.document.open(); 
                docprint.document.write('<html><head><title>your title</title>'); 
                docprint.document.write('</head><body><center>');
                docprint.document.write(oTable.innerHTML);
                docprint.document.write('</center></body></html>'); 
                docprint.document.close(); 
                docprint.print();
                docprint.close();
            }
        </script>
        <table id="tbl">
            <tr>
                <td> content1 </td>
                <td> content2 </td>
            </tr>
        </table>
        <input type="button" value ="print" id="printButton" onclick="javascript:printPage()"/>
    </body>
0
source

Source: https://habr.com/ru/post/1622216/


All Articles