views:

35

answers:

2

I know how to remove a tr from the table1 on click of delete. But i want to insert it in to another table .

How to do that?

here is the code i used to remove the tr.

<script languge="javascript" type="text/javascript">
  $(document).ready(function() {
 $('a.delete').click(function(e) {
  e.preventDefault();
  //Getting the TR to remove it in case of successful deletion
  var objRow = $(this).closest('tr');

  $.ajax({ 
   type: 'post',
   url: "<?= site_url('test/fndelete') ?>/",
   data: {"id":$(this).prev().text()},
   success: function(results) {
     if (results == "") { 
      objRow.remove();     
     } else {

    alert("Error!");
     }
   }  
  });
  return false;
 });
});
</script>
+1  A: 

i didnt test it but i think this will work:

success: function(results) { 
 if (results == "") {
  $('#table_where_all_the_deleted_stuff_goes').append('<tr>' + objRow.html() + '</tr>');
  objRow.remove();
 } else { 
  alert("Error!"); 
 }
}
antpaw
It worked. Thanks!
ASD
There are much more elegant solutions.
cballou
+1  A: 
$.ajax({ 
   type: 'post',
   url: "<?= site_url('test/fndelete') ?>/",
   data: {"id":$(this).prev().text()},
   success: function(results) {
     if (results == "") { 
         objRow.appendTo('#new_table tbody');
     } else {
         alert("Error!");
     }
   }  
});
cballou
Just to clarify this already great answer, appending an DOM element to a new container will also remove it from the old container at the same time. So there is no need to call the extra `remove` function and is why it is missing from this answer.
Doug Neiner