how to unbinb click when doing ajax modal call

Subject:

I created jquery to call modal and confirm delete, then inside the function there is another function to bind click of confirm 'yes' for delete.If you dont unbind the first call it will stay which means the second time you make a call again without page refresh it'll bind the inside function twice making two ajax calls.  So on and so forth, next it'll trigger 3 clicks.========================================look at //you need to unbind the click

$('.trash').click(function(){

    row = $(this).closest('tr');    name = $(this).closest('tr').children('td:first-child').html();    $('#modal-title').html('Delete Confirmation');    $('.modal-body p').html('This will permanently delete user '+name);    $('#modal-action-btn').html('Yes, delete!');    ID = $(this).closest('tr').find('a').attr('href').match(/id=(.*)/);                $('#modal-action-btn').click(function(){                        $.ajax({                type: 'POST',                url: '/assets/php/deluser.php',                data: 'id='+ID[1],                success: function(data){                    alert(data);                    row.hide();

                },                error: function(data){                    alert(data);                }            });//END OF AJAX        //you need to unbind the click        $('#modal-action-btn').unbind('click');        });//END OF CLICK modal-action-btn

}); //END OF TRASH CLICK FUNCTION

2015-05-07 13:50:21gstlouis

didn't pate properly.  try again:

references: http://stackoverflow.com/questions/17687868/why-does-jquery-ajax-post-twice-here

$('.trash').click(function(){

    row = $(this).closest('tr');
    name = $(this).closest('tr').children('td:first-child').html();
    $('#modal-title').html('Delete Confirmation');
    $('.modal-body p').html('This will permanently delete user '+name);
    $('#modal-action-btn').html('Yes, delete!');
    ID = $(this).closest('tr').find('a').attr('href').match(/id=(.*)/);
        
        $('#modal-action-btn').click(function(){
            
            $.ajax({
                type: 'POST',
                url: '/assets/php/deluser.php',
                data: 'id='+ID[1],
                success: function(data){
                    alert(data);
                    row.hide();

                },
                error: function(data){
                    alert(data);
                }
            });//END OF AJAX
        //you need to unbind the click
        $('#modal-action-btn').unbind('click');
        });//END OF CLICK modal-action-btn


}); //END OF TRASH CLICK FUNCTION

gstlouis
vote
2015-05-07 13:51:08