Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen for added table rows

Tags:

jquery

Lets say i have a table:

<table>
    <tr></tr>
</table>

$(function(){

    $('#btn').click(function(){
         $('table').append('<tr><input class="remove" value="remove"></tr>');
    });

    $('.remove').on('click', function(){

         $(this).parent().remove();
   });

});

Is there any way to bind a custom event handler that triggers whenever a row is added or removed, using jQuery?

Thanks

like image 380
Johan Avatar asked Feb 01 '12 12:02

Johan


1 Answers

Yes, you can bind custom event.

$('#table_id').bind('rowAddOrRemove', function(event){
    //do what you want.
});

And when you add or remove a row, you should trigger the event.

$('#table_id').trigger('rowAddOrRemove');
like image 161
xdazz Avatar answered Oct 07 '22 13:10

xdazz