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

5
  • 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. Commented Jan 18, 2016 at 15:52
  • yes, it is under content of the table Commented Jan 18, 2016 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. Commented Jan 18, 2016 at 15:59
  • 2
    I have given an answer here. stackoverflow.com/questions/25575996/… You may see this. Commented Feb 5, 2017 at 8:02
  • The solution suggested by @Md.HarunOrRashid is really helpful Commented Mar 12, 2020 at 8:46

7 Answers 7

122

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@sarower-jahan Thank you :)
In my case, before the click event was "$(".each_player").click(function(){ // my code goes here });" After modifying, "$("#playerGroupUI").on("click", ".each_player", function(){ // my code goes here });"
this fired the event multiple times instead of once!? 10 delete icons on the page and upon clicking one the delete confirm was fired twice
4
$(document).on("click",".activeAccount",function(e){
 // your code goes here
});

Comments

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">

Comments

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..
  }

Comments

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.

Comments

0

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

Comments

-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();
     });
   });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.