Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - remove table row <tr> by clicking a <td>

Tags:

jquery

I am making a table in which you can add aditional rows. When you add a row you can either save it or cancel it, by clicking cancel the row will be removed. It works with one row but when I create like six of them and click cancel the selected row wont be removed but the last row will. Here my Code so far. Does anyone know what I'm doing wrong?

<head>
  <script src="../jquery.js" type="text/javascript"></script>

  <script type="text/javascript">
  $(document).ready(function() {
  $(".edit").click(function() {
    var id = $(this).attr("id");
    alert("edit "+id);
  });
  $(".delete").click(function() {
    var id = $(this).attr("id");
    alert("delete "+id);
  });
  $("#newbutton").click(function() {
  var randomnumber=Math.floor(Math.random()*100);
    $("tr:last").after("<tr id="+randomnumber+"><td><form><input style='width: 80%'></form></td><td class=ok>OK</td><td id="+randomnumber+" class=cancel>Cancel</td></tr>").ready(function() {
      $("tr td .cancel").click(function() {
        $(this).remove();
      });
      $(".ok").click(function() {
        alert("OK!");
      });
    });
  })
}); 
  </script>
</head>
<table border=1 id=table>
<tr><th>Name</th></tr>
<tr><td>Bombai</td><td id=1 class=edit>edit</td><td id=1 class=delete>delete</td></tr> 
<tr><td>London</td><td id=2 class=edit>edit</td><td id=2 class=delete>delete</td></tr> 
<tr><td>Rom</td><td id=3 class=edit>edit</td><td id=3 class=delete>delete</td></tr> 
</table><label id=newbutton>New Place</label>
like image 504
elhombre Avatar asked May 29 '09 14:05

elhombre


People also ask

How do I remove a row from a table in jQuery?

The jQuery remove() method is used to remove a row from HTML table. jQuery remove() Method: This method removes the selected elements alongwith text and child nodes. This method also removes data and events of the selected elements.

How do I remove a row from a table in a table?

Right-click in a table cell, row, or column you want to delete. On the menu, click Delete Cells. To delete one cell, choose Shift cells left or Shift cells up. To delete the row, click Delete entire row.

How can remove dynamic row from table in jQuery?

append() and . remove() method we can dynamic add and delete row using jquery. append() method is used for append or add rows inside an HTML table and . remove() method to remove or delete table rows as well as all data inside it from the DOM dynamically with jquery.


2 Answers

Since you are dynamically adding rows to the DOM I'd suggest you use the live function:

$("tr td .cancel").live("click", function(){
  $(this).parent("tr:first").remove()
})
like image 171
duckyflip Avatar answered Sep 24 '22 11:09

duckyflip


<td><a class="delete" onclick ="delete_user($(this))">Delete</a></td>

in javascript

    function delete_user(row)
    {
        row.closest('tr').remove();


    }
like image 30
Uahmed Avatar answered Sep 23 '22 11:09

Uahmed