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

135
Vistas
Set type text inside cell javascript

I want to type text inside the cell but I tried to set attribute with td but it doesn't work. Please help me how can I set attribute to type text in the cell. Thank you for your help!

function addTable() {

    rn = window.prompt("Input number of rows", 1);
    cn = window.prompt("Input number of columns",1);

    var myTableDiv = document.getElementById("myDynamicTable");

    var table = document.createElement('TABLE');
    table.border = '1';
  
    var tableBody = document.createElement('TBODY');
    table.appendChild(tableBody);
  
    for (var i = 0; i < parseInt(rn, 10); i++) {
      var tr = document.createElement('TR');
      tableBody.appendChild(tr);
  
      for (var j = 0; j < parseInt(cn, 10); j++) {
        var td = document.createElement('TD');
        td.width = '75';
        td.appendChild(document.createTextNode("text"));
        tr.appendChild(td);
      }
    }
    myTableDiv.appendChild(table);
  }
addTable();
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

if you want to operate on tables, use corrects javascript instructions ...

const 
  DynTb = document.getElementById('myDynamicTable')
  rn    = +window.prompt("Input number of rows", 1) 
, cn    = +window.prompt("Input number of columns", 1)
  ;
if( isNaN(rn) || isNaN(cn))
  throw 'bad integer input value (s)'

const myTable = addTable(DynTb, rn, cn )

function addTable(tDiv, rn, cn) 
  {
  let 
    tab = tDiv.appendChild( document.createElement('table') )
  , tBy = tab.createTBody()
    ;
  for(let r=0;r<rn;++r)
    {
    let row = tBy.insertRow() 
    for(let c=0;c<cn;++c) 
      {
      row.insertCell().innerHTML = 
          `<input type="text" placeHolder="${r}-${c}">`
    } }
  return tab
  }


 
table  {
  border-collapse : collapse;
  margin          : 2em 1em;
  }
td {
  padding    : .2em;
  border     : 1px solid darkblue;
  }
input {
  width : 5em;
  }
<div id="myDynamicTable"></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to create an input element and then append that to td:

rn = window.prompt("Input number of rows", 1);
cn = window.prompt("Input number of columns", 1);

var myTableDiv = document.getElementById("myDynamicTable");

var table = document.createElement('TABLE');
table.border = '1';

var tableBody = document.createElement('TBODY');
table.appendChild(tableBody);

for (var i = 0; i < parseInt(rn, 10); i++) {
  var tr = document.createElement('TR');
  tableBody.appendChild(tr);

  for (var j = 0; j < parseInt(cn, 10); j++) {
    var td = document.createElement('TD');
    td.width = '75';
    var input = document.createElement('input');
    input.setAttribute('type', 'text');
    input.setAttribute('value', 'text');
    td.appendChild(input);
    tr.appendChild(td);
  }
}
myTableDiv.appendChild(table);
<div id="myDynamicTable">
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use contenteditable attribute: td.contenteditable = 'true';.

Depending on how you want to use this value, you might also insert an input element.

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