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

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

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 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