www.webdeveloper.com
Results 1 to 3 of 3

Thread: help with deletin rows

  1. #1

    help with deletin rows

    hi all
    i have taken from internet a script that adds and removes rows
    but i made a little changes to that script that is renumbering rows after deleting.
    but one problem arises. i can delete all rows by selecting at once except firts row. for ex. i have added 10 rows and i chose 9-th of them i delete them with no problem. but when i choose the first row it deletes one by one and displays error.
    my script is below
    PHP Code:
    <HTML>
    <
    HEAD>
        <
    TITLEAdd/Remove dynamic rows in HTML table </TITLE>
        <
    SCRIPT language="javascript">
        var 
    rowCount 0;
            function 
    addRow(tableID) {
     
    rowCount++;
                var 
    table document.getElementById(tableID);
     
                var 
    z=table.rows.length;
                if(
    rowCount>z)
                
    rowCount=z;
                var 
    row table.insertRow(rowCount);
     
                var 
    cell1 row.insertCell(0);
                var 
    element1 document.createElement("input");
                
    element1.type "checkbox";
                
    cell1.appendChild(element1);
     
                var 
    cell2 row.insertCell(1);
                
    cell2.innerHTML rowCount 1;
            
     
                var 
    cell3 row.insertCell(2);
                var 
    element2 document.createElement("input");
                
    element2.type "text";
                
    cell3.appendChild(element2);
     
            }
     
            function 
    delete() {
                try {
                var 
    table document.getElementById('dataTable');
                var 
    rowCount table.rows.length;
     
                for(var 
    i=0i<rowCounti++) {
                    var 
    row table.rows[i];
                    var 
    chkbox table.rows[i].cells[0].childNodes[0];
                    var 
    soz table.rows[i].cells[1];
                    if(
    null != chkbox && true == chkbox.checked) {
                        if(    
                        
    table.deleteRow(i);
                        
    rowCount--;
                        
    i--;
                    
                    }
                    
    table.rows[i].cells[1].childNodes[0].nodeValue=i+1;
                
                }
                
                
                }catch(
    e) {
                    
    alert(e);
                }
            }
     
        
    </SCRIPT>
    </HEAD>
    <BODY>
     
        <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
     
        <INPUT type="button" value="Delete Row" onclick="delete()" />
     
        <TABLE id="dataTable" width="350px" border="1">
            <TR>
                <TD><INPUT type="checkbox" name="chk"/></TD>
                <TD> 1 </TD>
                <TD> <INPUT type="text" /> </TD>
            </TR>
        </TABLE>
     
    </BODY>
    </HTML> 
    thanks in advance for attention

  2. #2
    Join Date
    Jan 2011
    Location
    San Francisco
    Posts
    19
    That's a tricky one, but I think I found it. If you delete the first row, i is 0 so the i-- line in the middle of the loop will make i = -1. Then, the call for table.rows[i].cells[1].childNode[0] produces the error because table.rows[-1] is undefined. Just replace that line with this:
    Code:
    		    if(i>=0) table.rows[i].cells[1].childNodes[0].nodeValue=i+1;
    and it seems to work fine for me.

    (as a side note, chrome at least considers the word "delete" reserved so don't use it as a function name . Also, you have a random line that read "if(" in that script, but I assume you fixed that )

  3. #3

    resolved

    Thank you very much.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles