-2

I have a table :

<table id="myTable" style="cursor :pointer;">
 <tr>
   <td>col 1</td><td>col2</td>
 </tr>
 <tr>
   <td>field</td><td>Field 2</td>
 </tr>
 <tr>
   <td>another field</td><td>one more field</td>
 </tr>
</table>

when I click on a row this code add a new row :

$("#myTable tr").click(function(){
   $(this).addClass('selected').siblings().removeClass('selected');
   var value=$(this).find('td:eq(1)').html();
   if (value) {
   alert('adding New Row !')
   $('#myTable').find('tbody:last').append('<tr><td>new Field</td><td>New Field</td></tr>');
    }
});

JSFiddle Demo

But now when I click on the new created row nothing append. Why another rowis not created ?

Is there a way to do that ?

SachaDee
  • 8,843
  • 2
  • 18
  • 29

1 Answers1

7

Use event delegation instead:

$("#myTable").on('click','tr',function() {});
Mohammad Usman
  • 30,882
  • 16
  • 80
  • 78
Milind Anantwar
  • 77,788
  • 22
  • 86
  • 114