Link to home
Create AccountLog in
Avatar of Sukeshjph
Sukeshjph

asked on

How to clear Datagrid items in Javascript

I want to clear datagrid in javascript before filling it, here is the code i have written but i am getting error please see below

function ClearTable()
{
      var tbl = document.getElementById('dgauthors').getElementsByTagName("tbody")[0];
      var row = tbl.rows.length
      for (var i=1,j=1;j<row;i++,j++)
      {
            if (tbl.rows.length == 2){i = 1;}
            tbl.deleteRow(i);
            if (tbl.rows.length == i) {i = 0;}
      }
}
ASKER CERTIFIED SOLUTION
Avatar of Salim Fayad
Salim Fayad
Flag of Lebanon image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of CRISIL
CRISIL

when i used given solution header get clear. instead of using this make modification in the given example its work fine for me.

// Clearing the existing contents of the Datagrid
function ClearTable()
{
      var tbl = document.getElementById('dgFamily').getElementsByTagName("tbody")[0];
      var row = tbl.getElementsByTagName("TR").length;
      for (var k=tbl.getElementsByTagName("TR").length -1 ;k > 0;k --)
      {
          tbl.removeChild(tbl.getElementsByTagName("TR")[k]);
      }
   
}
Crisil: How is this different from the solution given, other than making it more complex repeating dom acces and removeNode instead of table access and deleteRow?