14

I'm having trouble with a function in javascript and can't figure out why. It's really quite straight forward. I'm trying to delete all the rows in a html table. so I wrote:

function delete_gameboard(){
   var table = document.getElementById("gameboard");
   var rowCount = table.rows.length;
   for (var i = 0; i < rowCount; i++) {
      table.deleteRow(i);
   }
}

Yet, it'll only delete half of them. Can anyone see what's causing this strange behavior?

Hakan Fıstık
  • 14,367
  • 10
  • 94
  • 117
BooBailey
  • 507
  • 1
  • 8
  • 30

8 Answers8

35

Because when you delete the first row, the second row will become the first, it's dynamic.

You could do like:

while(table.rows.length > 0) {
  table.deleteRow(0);
}
xdazz
  • 154,648
  • 35
  • 237
  • 264
  • Helps to visualize these things or walk through it diagrammatically in pseudo code :) But yeah, that's an oversight one tends not to make more than once! – clearlight Jan 01 '17 at 08:14
5

Consider this:

index 0:  row #1
index 1:  row #2
index 2:  row #3
index 3:  row #4
index 4:  row #5

you delete index #2 (row #3), so the DOM engine adjusts the index keys and you end up with:

index 0:  row #1
index 1:  row #2
index 2:  row #4   <---hey! index #2 is still there
index 3:  row #5

You're basically digging a hole in the spot where you're standing, so as you dig deeper, you naturally sink deeper and never see any progress... until you run out of rows to delete in the table.

Marc B
  • 348,685
  • 41
  • 398
  • 480
4
function delete_gameboard(){
var table = document.getElementById("gameboard");
var rowCount = table.rows.length;
for (var i=rowcount-1; i >=0; i--) {
    table.deleteRow(i);
}
}

The index of the row changes when you delete it. Use a reverse loop. This will also be helpful if you are using any condition to delete rows. If you are deleting all rows,use the following

while(table.rows.length) {
  table.deleteRow(0);
}
Arun
  • 2,998
  • 3
  • 31
  • 55
3

If you delete the first row, the second row becomes the new first row.

I prefer to do this:

while(table.rows[0]) table.deleteRow(0);
Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566
2

The table rows is live, that is if you delete row 0 then the next row becomes row 0, just constantly delete row 0

function delete_gameboard(){
    var table = document.getElementById("gameboard");
    var rowCount = table.rows.length;
    for (var i=0; i < rowCount; i++) {
        table.deleteRow(0);
    }
}
Musa
  • 93,746
  • 17
  • 112
  • 129
1

You could just go:

    document.getElementById("gameboard").innerHTML = "";
DISSONANCE
  • 13
  • 3
0
function delRow()
  {
    var current = window.event.srcElement;
    //here we will delete the line
    while ( (current = current.parentElement)  && current.tagName !="TR");
    current.parentElement.removeChild(current);
  }
IMRUP
  • 1,415
  • 2
  • 11
  • 15
-2

If you are using JQuery in your code then the easiest way to delete the rows is using the following code:

$('#myTable tbody').html('');
dinesh782
  • 15
  • 3