Jump to content

Delete table row


jaymc

Recommended Posts

Im trying to use javascript to delete table rows

 

This works

 

len = tab.rows.length;

tab.deleteRow(len-1);

 

However, it will only delete the last row of the table

 

You can tell it to delete a row by its occurance in the table, for instance

 

len = tab.rows.length;

tab.deleteRow(3);

 

That will delete row 3. However, once you have deleted a row, row 5 becomes row 4, rows 4 becomes row 3 etc etc, therefor telling it which specific row to delete wont work

 

I need a way to delete a <TR> row by its ID

 

this must work in firefox

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/72286-delete-table-row/
Share on other sites

quick example. you can clean it up by removing the click events from the links etc

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function removeRow(row){
var r = document.getElementById('row:' + row);
r.parentNode.removeChild(r);
}
</script>
</head>

<body>
<table border="1" cellspacing="0" id="test_table">
  <tr id="row:0">
    <td> </td>
    <td> </td>
    <td><a href="#" id="del:0" onclick="removeRow(0);">del 0</a></td>
  </tr>
  <tr id="row:1">
    <td> </td>
    <td> </td>
    <td><a href="#" id="del:1" onclick="removeRow(1);">del 1</a></td>
  </tr>
  <tr id="row:2">
    <td> </td>
    <td> </td>
    <td><a href="#" id="del:2" onclick="removeRow(2);">del 2</a></td>
  </tr>
  <tr id="row:3">
    <td> </td>
    <td> </td>
    <td><a href="#" id="del:3" onclick="removeRow(3);">del 3</a></td>
  </tr>
</table>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/72286-delete-table-row/#findComment-366299
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.