Remove TABLE TR line with JavaScript

I am trying to delete a TR from a table using JavaScript and for some reason I don't know when I hit the delete button without deleting the full TR, but just deleting the img file.

function remove(rowid)  
{   
    var row = document.getElementById(rowid);
    var table = row.parentNode;
    while ( table && table.tagName != 'TABLE' )
        table = table.parentNode;
    if ( !table )
        return;
    table.deleteRow(row.rowIndex);
}

      

Please check the JS Fiddle for reference http://jsfiddle.net/h09wsrox/

thank

+3


source to share


2 answers


I needed to perform a similar function, but for the whole class. It should be similar. Not sure about your requirements, but I used JQuery to accomplish this.

function remove(rowid){
    $("#" + rowid).remove();
}

      



Source

+1


source


function removee(rowid)  
{   
    var row1 = document.getElementById(rowid);
    var table1 = row1.parentNode;
    while ( table1.tagName != 'TABLE' )
        table1 = table1.parentNode;
    if ( !table1 )
        return;
    table1.deleteRow(row1.rowIndex);

}

      

I have updated your js function name to remove

before removee

and also some small changes to it.

Here is DEMO



JavaScript in a separate DEMO block

Hope this helps you.

+4


source







All Articles