4

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>

4
  • Do you want to delete the first row every time when you click Delete button? Commented Jun 12, 2021 at 7:18
  • I want to delete each existing row when I click Delete.. so after to can update the table with new rows... Commented Jun 12, 2021 at 7:19
  • Why do you even have var chkbox = row.cells[0].childNodes[0];? it doesn't do anything except throw an error Commented Jun 12, 2021 at 7:20
  • @Cameron121341 Still not clear what exactly do you want? Commented Jun 12, 2021 at 7:21

6 Answers 6

2

To delete all rows just remove the html from the table

function deleteRow(tableID) {
  let table =  document.getElementById(tableID)
   table.innerHTML = "";
} 
Sign up to request clarification or add additional context in comments.

1 Comment

why is not possible to add new rows to table after table.innerHTML = ""; ? my ultimate goal is to add new different rows with different buttons.. so when I click on a button I delete each row and after that I add the new ones...
1

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>

Comments

1

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);;
}

1 Comment

why is not possible to add new rows to table after table.innerHTML = ""; ? my ultimate goal is to add new different rows with different buttons.. so when I click on a button I delete each row and after that I add the new ones...
0

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>

2 Comments

why is not possible to add new rows to table after table.innerHTML = ""; ? my ultimate goal is to add new different rows with different buttons.. so when I click on a button I delete each row and after that I add the new ones...
Hi @Cameron121341, once you delete all rows in a table by clicking on a button, using table.innerHTML = "". You can add new rows again to the same table, above code works for this scenario. Please elaborate your question about this point "new different rows with different buttons"? If the above answer does not meet your requirements.
0

Maybe you should change

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

into:

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

Comments

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.

1 Comment

why is not possible to add new rows to table after table.innerHTML = ""; ? my ultimate goal is to add new different rows with different buttons.. so when I click on a button I delete each row and after that I add the new ones...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.