Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda