datatables jquery click event not working after pagination

ajay p picture ajay p · Jan 18, 2016 · Viewed 32.5k times · Source

I am using http://datatables.net/

<button class='btn btn-success activeAccount'>Activate Account</button>

I trigger ajax call on onclick event, below is ajax call code:

$(".activeAccount").click(function() {
  var intCounselorId = $(this).parent().parent().find('input[class="counselorId"]').attr("value");
  var intOwnerId = $(this).parent().parent().find('input[class="userID"]').attr("value");
  var strAction = 'activateAccount';
  performAction(intCounselorId, intOwnerId, strAction);
});

function performAction(intCounselorId, intOwnerId, strAction) {
  $.ajax({
      url: '/admin/counselormanagement/expertmanagementgridaction',
      data: 'intCounselorId='+intCounselorId+'&intOwnerId='+intOwnerId+'&strAction='+strAction,
      type: "POST",
      async:false,
      success: function(intFlag) {
        if(intFlag == 1){
          location.reload();
        }
      }
  });
}

I'm trying to run an onclick event which works normally on page one, but as soon as I go to page 2 (or any other) it stops working.

I'm using jquery-1.10.2.min.js and 1.9.4 version of datatable

Answer

squaleLis picture squaleLis · Jan 18, 2016

Because the event is attached only to existing elements.

You should change it to:

$("#tableId").on("click", ".activeAccount", function(){
   // your code goes here
});

Read more in the documentation of jQuery.on.