jQuery DataTables: Why click event handler does not work

This is one of the most common problems users have with jQuery DataTables plug-in and it has very simple solution if you understand why it occurs.


Table below demonstrates the problem with attaching event handlers incorrectly. Clicking on “Edit” button should open a modal dialog.

Event handler is attached as follows:

$('#example .btn-edit').on('click', function(){
   // ...

However if you go to a different page, sort or filter the table, “Edit” button stops working for those records that were previously hidden.


After the table is initialized, only visible rows exist in Document Object Model (DOM). According to jQuery on() documentation:

Event handlers are bound only to the currently selected elements; they must exist at the time your code makes the call to .on()

Our code above worked only for the first page elements because elements from other pages were not available in DOM at the time the code was executed.


The solution is to use event delegation by providing selector for target element as a second argument in on() call, see example below. According to jQuery on() documentation:

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time.

$('#example').on('click', '.btn-edit', function(){
   // ...

We attach event handler to the table '#example' and specify a selector for the button '.btn-edit' as an additional parameter for on() method. Event handler will now be called for all buttons whether they exist in DOM now or added at a later time.

Table below demonstrates the working solution.

You May Also Like


  1. A 2015 post has finally given me a clue…your code sample isn’t identical to mine, but the problem is. I have a function call to a .change when a select box is changed. Works on the first page, not on subsequent. The .on samples you have don’t work and changing to .change or .on ('change',... etc don’t work.


    $("select#closeStatus").on('change', '.btn-details', function () {
            $("select#closeStatus").change(function () {

    Thoughts? What am I missing?

    1. Please use the following pattern:

      $(tableSelector).on('change', elementSelector, function(e){
         // Your code here

      Using id attribute closeStatus for more than one select element is not recommended, you need to use class instead.

      For example, for table with ID example and multiple select elements with class closeStatus:

      $('#example').on('change', 'select.closeStatus', function(e){
         // Your code here
  2. My problem was similar to @Chad M and your answer worked for me. Thank you so much @Michael Ryvkin !

Leave a Reply

You may use simple HTML to add links or lists to your comment. Also use <pre><code class="language-*">...</code></pre> to mark up code snippets. We support language-js, language-markup and language-css for comments.

This site uses Akismet to reduce spam. Learn how your comment data is processed.