Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Posts
    143
    Thanks
    0
    Thanked 0 Times in 0 Posts

    why the deleteRow() dont delete all the tables rows?

    Hello
    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 :
    ------------------------------------------------------------------------------
    <HTML>
    <HEAD>
    <script>
    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++){
    document.getElementById("TableFirst").deleteRow(j);
    }
    }
    }
    </script>
    </HEAD>
    <BODY>
    <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>
    </BODY>
    </HTML>
    --------------------------------------------------------------------------------
    what im doing wrong here?
    thanks

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    var t = document.getElementById( "TableFirst" );
    while( t.rows.length )
    {
        t.deleteRow( 0 );
    }
    or
    Code:
    for ( var i = t.rows.length; i >= 0; i-- )
    {
        t.deleteRow( i );
    }
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •