Karthik Malla
Karthik Malla

Reputation: 5800

Delete TABLE row TR with JavaScript

I am trying to delete the TR from the table using JavaScript and I don't know for some reasons when I click on delete button its not deleting the complete TR instead its 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/

Thanks

Upvotes: 3

Views: 408

Answers (2)

Sam1604
Sam1604

Reputation: 1489

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 to removee and also some small changes in it.

Here is a DEMO

JavaScript in separate block DEMO

I hope it helps you.

Upvotes: 4

japtain.cack
japtain.cack

Reputation: 132

I had to do a similar function but to a whole class. It should be similar though. Not sure of your requirements, but I used JQuery to accomplish this.

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

Source

Upvotes: 1

Related Questions