Click to See Complete Forum and Search --> : loops


z_mirza
06-05-2003, 07:14 PM
for (var i = 0; i < ElementTable.rows.length; i++)
{
ElementTable.deleteRow();
}


my problem is that i increases, and ElementTable.rows.length decreases because on each run, a row is deleted so eventually i = 6 and ElementTable.rows.length = 6 ending my loop, so i need to some how keep i going up to 12 and ElementTable.rows.length go down to 0.
any ideas?

Khalid Ali
06-05-2003, 07:42 PM
How about doing it this way.

var len = ElementTable.rows.length;

for (var i = 0; i <len i++){
ElementTable.deleteRow();
}


You should make a habit of doing it this way,on small scale you will never notice the difference but if there are hndreds o records to be deleted,its pretty efficient in comparison.

JHL
06-05-2003, 07:46 PM
Why not use a while loop?

while(ElementTable.rows.length!=0)
{
ElementTable.deleteRow();
}

z_mirza
06-05-2003, 07:46 PM
oh thanx Khalid Ali i understand why you did that and it makes sense to do it this way, i think ill stick with for loop thanx any way

Khalid Ali
06-05-2003, 08:05 PM
:D
You are welcome...
Yep practicing that will solve allots of unseen problems..:-)