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

802
Visualizações
Auto-increment the ID in an html table with javascript

I have a basic table structure:

<tr>
   <th>ID</th>
   <th>Name</th>
</tr>
<tr>
    <td>1</td>
    <td>John</td>
</td>

My javascript function:

function newRow() {

  var tbodyRef = document.getElementById('myTable').getElementsByTagName('tbody')[0];
  var lastRow = tbodyRef.rows[tbodyRef.rows.length - 1];
  var lastCell = lastRow.cells[lastRow.cells.length -1];

  // Insert a row at the end of table
  var newRow = tbodyRef.insertRow();
  
  // Insert a cell at the end of the row
  var newCell = newRow.insertCell();
  newCell.innerHTML = lastCell.id++;
  newCell = newRow.insertCell();
  newCell.innerHTML = "Alex";
}

Currently calling the function only generates '0' in ID column, how can I increment it?

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

Try to run the following:

function newRow() {

  var tbodyRef = document.getElementById('myTable').getElementsByTagName('tbody')[0];
  var lastRow = tbodyRef.rows[tbodyRef.rows.length - 1];
  var lastCell = lastRow.cells[lastRow.cells.length - 1];

  // Insert a row at the end of table
  var newRow = tbodyRef.insertRow();
  
  // Insert a cell at the end of the row
  var newCell = newRow.insertCell();
  newCell.innerHTML = lastRow.rowIndex+1;
  newCell = newRow.insertCell();
  newCell.innerHTML = "Alex";
}

A working example with JS Fiddle: http://jsfiddle.net/o30gxeda/1/

about 4 years ago · Santiago Gelvez Relatório

0

Check my below snippet I used Template Literals and querySelector() method with easy way to achieve and count rows length and increment by 1.

Helpful Link:
https://www.w3schools.com/js/js_string_templates.asp https://www.w3schools.com/jsref/met_document_queryselector.asp

function newRow() {
    var tbodyRef = document.querySelector('#myTable tbody');
    tbodyRef.innerHTML+=`
    <tr>
        <td>${tbodyRef.rows.length + 1}</td>
        <td>Alex</td>
    </tr>`;
}
<table id="myTable" width="100%" border="1" cellpadding="10" cellspacing="0">
    <thead>
        <tr>
            <th align="left">ID</th>
            <th align="left">Name</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>John</td>
        </tr>
    </tbody>
</table>

<br>
<button type="button" onclick="newRow()">Add New Records</button>

about 4 years ago · Santiago Gelvez 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