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

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

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 Relatório

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 Relatório

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