Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda