Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
Printing a table in HTML

I have dispalyed a table in my web page i want to print it on the button click .My button is not working my code goes as follows.

<button id="button1">Print me</button>  
                        
    <table id="printTable" class="display responsive-table " style=" margin-bottom:50px; border-radius: 4px;"  >
    <thead>
    <tr >
    <th>Sl.No.</th>
    <th>Description</th>
    <th>Balance</th>
    
    </tr>
    </thead>
    <tbody>
    
    <script>
    function printData()
    {
        var divToPrint=document.getElementById("printTable");
        newWin= window.open("");
        newWin.document.write(divToPrint.outerHTML);
        newWin.print();
        newWin.close();
    }

    $('#button1').on('click',function(){
    printData();
    })
    </script>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

if you are not using jquery, do this :

        function printData() {
            var divToPrint = document.getElementById("printTable");
            newWin = window.open("");
            newWin.document.write(divToPrint.outerHTML);
            newWin.print();
            newWin.close();
        }

        const btn = document.getElementById("button");
        btn.addEventListener('click', () => printData())

also you must close all tags (table, tbody..)

about 4 years ago · Juan Pablo Isaza Report

0

Please use the below code. I have comment out the newWin.close() function so you will see the result.

<button id="button">Print me</button>   
                        
    <table id="printTable" class="display responsive-table " style=" margin-bottom:50px; border-radius: 4px;"  >
    <thead>
    <tr >
    <th>Sl.No.</th>
    <th>Description</th>
    <th>Balance</th>
    
    </tr>
    </thead>
    <tbody>
    </tbody>
  </table>
    
    <script>
    function printData()
    {
      
        var divToPrint=document.getElementById("printTable");
        newWin= window.open("");
        newWin.document.write(divToPrint.outerHTML);
        newWin.document.close();
        newWin.print();
        //newWin.close();
    }

    document.querySelector("#button").addEventListener("click", function(){
      printData();
    });
    
    </script>
about 4 years ago · Juan Pablo Isaza Report

0

You don't have closing table tag. There is an opening tbody tag instead.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!