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

219
Vistas
Make table via value

How do I do that JavaScript will print in my HTML page a table via the value the user will choose?

That the JS script:

let numCol = document.getElementById('txtColumns').value;
let numRow = document.getElementById('txtRows').value;
let go = document.getElementById('btn');
let table = document.getElementById('table');

let td = "<td></td>" * numCol;
let tr = ("<tr>" + td + "</tr>") * numRow;

go.addEventListener('click', function(){
    table.innerHTML = tr;
})

That the HTML code:

<html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <table class="workTable">
            <tr>
                <td>
                    <input type="number" placeholder="Columns Number" id="txtColumns">
                </td>
                <td>
                    <input type="number" placeholder="Rows Number" id="txtRows">
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <button id="btn">
                        Print
                    </button>
                </td>
            </tr>
            <div>
                <table id="table">
                    <!--Here I want to print the table-->
                </table>
            </div>
        </table>
        <script src="script.js"></script>
    </body>
</html>

At first I thought about that way with the script but its only appear as a NaN and not table...

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The following syntax:

let td = "<td></td>" * numCol;

does not produce numCol cells, so the following syntax:

let tr = ("<tr>" + td + "</tr>") * numRow;

does not produce numRow rows also.

So, the whole source code should be:

let go = document.getElementById('btn');
let table = document.getElementById('table');

go.addEventListener('click', () => {
  let numCol = document.getElementById('txtColumns').value; //Get the value of txtColumns at the button click moment.
  let numRow = document.getElementById('txtRows').value;

  let td = "",
    tr = "";
  for (let i = 0; i < numCol; i++) {
    td = td + "<td></td>";
  }
  for (let i = 0; i < numRow; i++) {
    tr = tr + "<tr>" + td + "</tr>";
  }

  table.innerHTML = tr;
})
<table class="workTable">
            <tr>
                <td>
                    <input type="number" placeholder="Columns Number" id="txtColumns">
                </td>
                <td>
                    <input type="number" placeholder="Rows Number" id="txtRows">
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <button id="btn">
                        Print
                    </button>
                </td>
            </tr>
            <div>
                <table id="table" border="1">
                    <!--Here I want to print the table-->
                </table>
            </div>
        </table>

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