insertrow into a table javascript code example

Example: javascript insert row at end of table

<table id="ThisTable">
  <tr><th>Head1</th><th>Head2</th><th>Head2</th></tr>
  <tr><td>Data1</td><td>Data1</td><td>Data1</td></tr>
  <tr><td>Data2</td><td>Data2</td><td>Data2</td></tr>
</table>
<script>
  var table = document.getElementById("ThisTable");
  var row = table.insertRow(-1);
  <!-- Filling the new created cell -->
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);
  var cell3 = row.insertCell(2);
  cell1.innerHTML = "NEW CELL1";
  cell2.innerHTML = "NEW CELL2";
  cell3.innerHTML = "NEW CELL3";
</script>
<!-- The (-1) indexes the last position of the table -->

Tags:

Html Example