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

92
Visualizações
Trying to add textbox dynamically to table when rows are added

I need to create a table using only JavaScript. The table is created dynamically so I can add extra rows if I needed too. However, each row on the table needs to have a text box.

When I try to do it this way (shown below) the table loads but says [object HTML Input Element] where the text box should be. I think it's because element.innerHTML is trying to change it to a string.

Can anyone help with another way to add the text box to the table?

for (var i = 0; i < tableRows.length; i++) {

  var table = document.getElementById("myTable");
  var row = table.insertRow(table.rows.length);

  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);

  //element from array    

  //input box
  var x = document.createElement("INPUT");
  x.setAttribute("type", "text");

  cell1.innerHTML = "object.Section"; //adds content from array outside the loop
  cell2.innerHTML = document.body.appendChild(x); //adding text box
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Instead of using innerHTML, use appendChild directly on cell2 like this: cell2.appendChild(x);

So, your code will be:

for (var i = 0; i < tableRows.length; i++) {

  var table = document.getElementById("myTable");
  var row = table.insertRow(table.rows.length);

  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);

  //element from array    

  //input box
  var x = document.createElement("INPUT");
  x.setAttribute("type", "text");

  cell1.innerHTML = "object.Section"; //adds content from array outside the loop
  cell2.appendChild(x); //adding text box
}
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