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-problem .btn-details').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.

Name Position Office Extn. Start date Salary
Name Position Office Extn. Start date Salary


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

So the code below works only for the first page elements because elements from other pages were not available in DOM at the time the code was executed.

$('#example-problem .btn-details').on('click', function(){


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-problem').on('click', '.btn-details', function(){

We attach event handler to the table '#example-problem' and specify a selector for the button '.btn-details' 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.

Name Position Office Extn. Start date Salary
Name Position Office Extn. Start date Salary

Related posts


  1. Thanks Michael,

    Are you able to get this solution to work if you have column searching enable? Everything in your example works great until I enable the column sorting options. When the grid initially renders everything works fine, however once I filter a specific column by some text I have to click twice to invoke the on click event.


    1. Wade, I can’t replicate the behavior you’re talking about. For example, I search for “Accountant”, there are two records shown. Click on “Edit” button works as expected, only one click is required.

      1. I’m also getting this issue.
        Michael is correct that it doesn’t affect the default searching abilities (searching all columns) but it affects the individual column filtering. It’s strange, after the second click it fires the listener…

          1. Originally datatables suggested binding both a keyup and onchange (for mouse copy and pastes) event handlers to the search filter text input.

            // Old doc with issue.
            $('<input type="text").on('keyup change', function () {
            //Which has now been replaced with
            $('<input type="text").keyup(function () {

            I see your solution still uses the keyup which got me intrigued…

            Turns out when I was clicking the button the onchange for the input was firing and redrawing the table instead of the button on click. On the second click of the button the event handler would finally fire.

            This line in your code prevents this, if you remove it you'll get the same issue:
            if ( that.search() !== this.value ) {


            I would have thought both the input change and button click would have fired? Very strange…

            1. Got it… so the change event fires first, redrawing the table… then the click on the table attempts to fire but of course there’s no buttons as the table is being redrawn at that point…
              Thanks for your help!

Leave a Reply

Your email address will not be published.