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

133
Visualizações
.insertRow() is not working well when <tbody> is empty

when I try to insert a row to empty <tbody> of my table, it added row to <thead> that is not empty, but not to <tbody>.

Example:

<!DOCTYPE html>
<html>
<body>
  <table id="myTable">
    <thead style="background-color: red;">
      <tr>
        <td>A</td>
        <td>B</td>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
  <br>
  <button type="button" onclick="myFunction()">Try it</button>
  <script>
    function myFunction() {
      var table = document.getElementById("myTable");
      var row = table.insertRow();
      var cell1 = row.insertCell(0);
      var cell2 = row.insertCell(1);
      cell1.innerHTML = "NEW CELL1";
      cell2.innerHTML = "NEW CELL2";
    }
  </script>
</body>
</html>

EDIT

Solution: <tbody><tr></tr></tbody>, but it's not very clear...

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

0

A table has three parts of: thead, tbody and tfoot. There is no reason your code to insert a new row to tbody, as you want. if you want to add a row in tbody you need to mention it. In your coed make a little change:

function myFunction() {
  var table = document.getElementById("myTable").getElementsByTagName('tbody')[0];
  var row = table.insertRow();
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);
  cell1.innerHTML = "NEW CELL1";
  cell2.innerHTML = "NEW CELL2";
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use var table = document.querySelector("#myTable tbody"); if you want to add a row in tbody you need to mention the exact location like i did with document.querySelector("#myTable tbody");.

<!DOCTYPE html>
<html>

<body>
  <table id="myTable">
    <thead style="background-color: red;">
      <tr>
        <td>A</td>
        <td>B</td>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
  <br>
  <button type="button" onclick="myFunction()">Try it</button>
  <script>
    function myFunction() {

      var table = document.querySelector("#myTable tbody");
        
      var row = table.insertRow();

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



      cell1.innerHTML = "NEW CELL1";
      cell2.innerHTML = "NEW CELL2";
    }
  </script>
</body>

</html>

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