最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Html add and delete rows dynamically with javascript - Stack Overflow

programmeradmin1浏览0评论

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>

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>

Share Improve this question asked Jun 12, 2021 at 7:06 Cameron121341Cameron121341 2602 silver badges11 bronze badges 4
  • Do you want to delete the first row every time when you click Delete button? – DecPK 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... – Cameron121341 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 – Kinglish Commented Jun 12, 2021 at 7:20
  • @Cameron121341 Still not clear what exactly do you want? – DecPK Commented Jun 12, 2021 at 7:21
Add a ment  | 

6 Answers 6

Reset to default 2

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

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>

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.
发布评论

评论列表(0)

  1. 暂无评论