Select and delete a row

To add lines, I wrote code like this

$('#tab1 tbody ').append('<tr id='+i+'><td>'+k+'</td><td>'+l+'</td><td>'+m+'</td></tr>');

in the previous snippet iis a global value.

Now, if I try to select a newly added row, it does not recognize .. for selection, I wrote like this

$('#tab1 td').click(function(){
 alert(i);
 $(this).parent().remove();
});

Do you see errors?

+5
source share
4 answers

I think your td elements have invalid identifiers. ID must not begin with numbers . Try adding static text to i.

You should also use the event to get the items generated in js .live()

$('#tab1 td').live("click", function(){
   alert(i);
   $(this).parent().remove();
});
+6
source

click <td> , , .click().

.live(), , , , .

+4

Edit

$('#tab1 td').click(function(){

to

$('#tab1 td').live('click', function(){
+4
source

This is added at runtime, so you should use live instead of clicking

http://api.jquery.com/live/

$('#tab1 td').live('click', function() {
  alert(i);
 $(this).parent().remove();
});
+4
source

All Articles