View Full Version : why the deleteRow() dont delete all the tables rows?

09-24-2003, 01:06 PM
i have simple script ,one function builds rows and one (supposed to ..) delete all the rows
but the delete rows function dont delete all the rows , im using the deleteRows(n) dhtml function
here is the code :
function setTable(){
var oRow,oCellText;
var arrNames = "blah1|blah2|blah3|blah4|";
arrNames = arrNames.split("|");
for(var i=0;i<arrNames.length-1;i++){
oRow = TableFirst.insertRow();
oCellText = oRow.insertCell();
oCellText.innerHTML = arrNames[i];
function delTable(){
if(TableFirst.rows.length > 0){
var rowLength = parseInt(TableFirst.rows.length);
for(var j=0;j<rowLength;j++){
<input type="button" onclick="setTable();" value="build"><br>
<input type="button" onclick="delTable();" value="Delete"><br>
<table id="TableFirst" border="1" cellspacing="0" cellpadding="0" width="100%" class="font"></table>
what im doing wrong here?

Roy Sinclair
09-24-2003, 04:16 PM
It's real simple. You're deleting from the wrong end.

More specifically, you set up a table with four rows. Now when you delete the rows you're starting by telling it to delete row 1 which deletes the first row. Then you tell it to delete row 2 so it deletes what was the third row originally but is now row two because the original row one is now gone. Your script then fails because you next try to delete row three but since the table is now only two rows long there is no row three.

Turn on the javascript error messages in your browser, if you'd had them on you'd likely have figured this out yourself already.

09-24-2003, 06:07 PM
var t = document.getElementById( "TableFirst" );
while( t.rows.length )
t.deleteRow( 0 );
for ( var i = t.rows.length; i >= 0; i-- )
t.deleteRow( i );

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum