35

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

gawi
  • 2,843
  • 4
  • 29
  • 44
ajay p
  • 639
  • 1
  • 7
  • 10
  • 2
    Is the `.activeAccount` button inside the content of the table? Also, note that it's really bad practice to use `async: false`, and the `location.reload()` in the success handler completely negates the entire point of making an AJAX request in the first place. – Rory McCrossan Jan 18 '16 at 15:52
  • yes, it is under content of the table – ajay p Jan 18 '16 at 15:59
  • In that case you need to use a delegated event handler, as per @squaleLis's answer. You really should get rid of `async: false` and the `location.reload()` though. – Rory McCrossan Jan 18 '16 at 15:59
  • 2
    I have given an answer here. http://stackoverflow.com/questions/25575996/ajax-call-not-working-on-datatables-pages-except-first-page You may see this. – Md. Harun Or Rashid Feb 05 '17 at 08:02
  • The solution suggested by @Md.HarunOrRashid is really helpful – Ogbonna Vitalis Mar 12 '20 at 08:46

7 Answers7

120

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.

Rishi Jagati
  • 626
  • 1
  • 6
  • 28
squaleLis
  • 6,116
  • 2
  • 22
  • 30
4
$(document).on("click",".activeAccount",function(e){
 // your code goes here
});
Mahmut Aydın
  • 831
  • 13
  • 21
2

I had the same issue. Every time my AJAX function(modalContentAjaxRefresh) update the table the paging stop. SO I just had to change my code from :

From :

$('.modal-toggle').off('click', modalContentAjaxRefresh).on('click', modalContentAjaxRefresh);

to:

$('#DataTables_Table_0_wrapper').on("click", ".modal-toggle", modalContentAjaxRefresh);

My button inside datatable is :

< a title="Edit" class="btn btn-xs btn-info modal-toggle"
data-style="zoom-in" href="/setting/account/{{account_turn.uuid}}/update" data-toggle="modal" data-target="#editAccount" wecare-method="GET">

2

As @squaleLis said, the event is attached to only existing elements.

So, in my case I defined onclick event for the button and called it.

  <button class='btn btn-success activeAccount' onclick="YourMethod();">Activate Account</button>

  function YourMethod() {
     ....same code..
  }
Neelima Ediga
  • 346
  • 1
  • 5
  • 19
0
$("#product-list").on("click",".btn-delete-product",function(e){
    e.preventDefault();
    var prodId     =   $(this).attr("product-id"); 
    .... code to delete the record from the db...
  });

product-list is the table where data gets loaded and has paging enabled.

This works perfectly for me.

hackernewbie
  • 1,606
  • 20
  • 13
0

I thinks a good and easy solution is to use drawCallback option

Harshil Modi
  • 397
  • 3
  • 8
  • 15
-1

The main important thing is to reassign the elements when click the pagination.

//function to assign event
var assign_actions = function(){
  //Some code  
}
  //when the page is ready
  $( document ).ready(function() {
    assign_actions();
    //Code to assign event when paginate
     $('.paginate_button').on('click', function(){
       assign_actions();
     });
   });
fp007
  • 412
  • 1
  • 4
  • 18