Html add and delete rows dynamically with javascript

Viewed 821

I want to have a table with no rows initially and after that to create each row dynamically and also to be able to delete every row. I got Cannot read property 'childNodes' of undefined Please let me know how it should be done, Thank you.

function addRow() {
  var table = document.getElementById("myTable");
  var row = table.insertRow(0);
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);
  cell1.innerHTML = "NEW CELL1";
  cell2.innerHTML = "NEW CELL2";
}

function deleteRow(tableID) {
  try {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;

    for (var i = 0; i < rowCount; i++) {
      var row = table.rows[i];
      var chkbox = row.cells[0].childNodes[0];

      table.deleteRow(i);
      rowCount--;
      i--;
    }
  } catch (e) {
    alert(e);
  }
}
<!DOCTYPE html>
<html>

<head>
  <style>
    table,
    td {
      border: 1px solid black;
    }
  </style>
</head>

<body>

  <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

  <table id="myTable">
    <TR>

    </TR>
  </table>
  <br>

  <button type="button" onclick="addRow()">Add</button>
  <button type="button" onclick="deleteRow('myTable')">Delete</button>

</body>

</html>

6 Answers

To delete all rows just remove the html from the table

function deleteRow(tableID) {
  let table =  document.getElementById(tableID)
   table.innerHTML = "";
} 

The function deleteRow() looks misleading. As you pass the entire table to delete. So why not adding a button "Delete" to each row you create. If you want to delete all rows see the modification, I've changed your function deleteRow(tableID) to deleteAllTableRows(tableID)

function deleteThisRow() {
  this.closest('tr').remove()
}

function addRow() {
  /* create a span element ("Delete") with an event listener "deleteThisRow" */
  let deleteButton = document.createElement('span');
  deleteButton.innerHTML = 'Delete';
  deleteButton.addEventListener('click', deleteThisRow);
  
  var table = document.getElementById("myTable");
  var row = table.insertRow(0);
  var cell0 = row.insertCell(0);
  var cell1 = row.insertCell(1);
  var cell2 = row.insertCell(2);
  cell0.innerHTML = "NEW CELL1";
  cell1.innerHTML = "NEW CELL2";
  cell2.append(deleteButton); // add the "Delete" button to each row
}


function deleteAllTableRows(tableID) {
  var rows = document.querySelectorAll('#' + tableID + ' tr ')
  rows.forEach( row => { 
    row.remove()
  });
}
table,
td {
  border: 1px solid black;
}
<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

<table id="myTable"></table>
<br>

<button type="button" onclick="addRow()">Add</button>
<button type="button" onclick="deleteAllTableRows('myTable')">Delete all rows</button>

The deleteRow() method removes the row at the specified index from a table. Maybe you should change

for (var i = 0; i < rowCount; i++) {
  table.deleteRow(0);;
}

Maybe you should change

var chkbox = row.cells[0].childNodes[0];

into:

var chkbox = row.childNodes[0].childNodes[0];
  1. tableID.innerHTML=""; , is an easy way to clear all elements inside that table.
  2. If the table is empty, there is no childNodes. Then it is undefined.

You can achieve it by simply changing your deleteRow() function like below. Whereas row.cells[0].childNodes[0] gives you error. row.cells[0] gives you HTML code, similar to like this - <td>NEW CELL1</td>. The childNode of that HTML does not have any information. So, always throws an error.

function addRow() {
  var table = document.getElementById("myTable");
  var row = table.insertRow(0);
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);
  cell1.innerHTML = "NEW CELL1";
  cell2.innerHTML = "NEW CELL2";
}

function deleteRow(tableID) {
  document.getElementById(tableID).innerHTML = "";
}
<!DOCTYPE html>
<html>

<head>
  <style>
    table,
    td {
      border: 1px solid black;
    }
  </style>
</head>

<body>

  <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

  <table id="myTable">
    <tr>

    </tr>
  </table>
  <br>

  <button type="button" onclick="addRow()">Add</button>
  <button type="button" onclick="deleteRow('myTable')">Delete</button>

</body>
</html>

Related