With DataTable I can order, list, do pagination but I want to detect when the pagination changes, I've seen the API but the only one I can do is change the page but no detect this change.
-
Is there a specific reason behind detecting the page change? – Praveen Sep 13 '11 at 19:58
-
1Yes, I want to send to the server what pages has been viewed by the user. – Felix Sep 13 '11 at 20:28
12 Answers
You may use fnDrawCallback or fnInfoCallback to detect changes, when next is clicked both of them are fired.
But beware, page changes are not the only source that can fire those callbacks.
For DataTables 1.10.0+
The option is now drawCallback

- 397
- 3
- 8
- 15

- 10,062
- 15
- 61
- 69
-
THis is the only solution that worked, but as he mentioned these events are triggered on many other things including fnAddData. – Apr 22 '13 at 17:54
-
-
With page.dt event, you handle it before the page change. if you put your code in the fnDrawCallback, It will execute after the page change. – federico__ Oct 04 '21 at 09:22
Paging events are handled in this way,
$(document).ready(function() {
$('#example')
.on( 'order.dt', function () { console.log('Order' ); } )
.on( 'search.dt', function () {console.log('Search' ); } )
.on( 'page.dt', function () { console.log('Page' ); } )
.dataTable();
} );
documented in the official website, here http://www.datatables.net/examples/advanced_init/dt_events.html
The length.dt event is fired whenever the table's page length is changed
$('#example').dataTable();
$('#example').on( 'length.dt', function ( e, settings, len ) {
console.log( 'New page length: '+len );
} );
http://datatables.net/reference/event/length
More events here

- 557
- 5
- 9
-
1Thanks! Im used `.on( 'page.dt', function () { console.log('Page' ); } )` very well! – Alberto Cerqueira Nov 27 '17 at 16:15
-
1When i used `$('#example').on( 'page.dt', function () { mySpecificFunction(); } )` it is only calling my function, it does not changing the page. even my function works without any error. – Salmanul Faris Jun 26 '22 at 18:58
I got it working using:
$('#id-of-table').on('draw.dt', function() {
// do action here
});

- 1,456
- 21
- 40
-
Please provide an explanation of how this code answers the question. This will help the OP and other future searchers of this question. – SnareChops Jan 26 '16 at 01:23
-
-
1This solution works perfectly for me. For more information on the event "draw.dt", please visit [link](https://datatables.net/reference/event/draw). – Decoder.Ninja May 23 '19 at 16:41
-
This worked for me because this is called *after* the new page rows are inserted. Using page.dt was called after the pagination button was clicked but before it replaces the new rows in the table. – raphael75 Feb 03 '21 at 21:11
-
-
This working perfect for me! As the page.dt event is before the new page is finished loading, but with this one is executes my code after the new page loading is finsihed. – Kalia Martin Reina Apr 11 '23 at 11:39
IF you have a version greater than 1.8, you can use this to hit the page change events:
$('#myTable').on('page', function () {...} );
Hope this helps!
UPDATE:
Some comments have pointed out that using .live() instead of .on() worked for them. Be aware of that you should try both and see which one works best in your particular circumstance! (I believe this may have to do with your version on jQuery, but please comment if you find another reason!)

- 5,968
- 13
- 62
- 101
-
1Small update to your solution. The .on() method did not work for me. I changed it to .live() and it worked great. – edwardrbaker Oct 08 '12 at 16:53
-
2I think the .live() v. .on() may deal with your version on jQuery / DT. I've been using .on() and it works for me. @ScottBeeson, what is your specific issue? If you're going to vote down my answer, please at least provide an example that proves it is not working in your particular circumstance. It seems to work for others and myself. – streetlight Apr 22 '13 at 18:57
-
I'm wondering how to trigger the same function when the first page is loaded. – Kimball Robinson Dec 30 '13 at 22:19
-
4Since datatables 1.10 you need namespacing --> http://next.datatables.net/manual/events. – Jacob van Lingen May 23 '14 at 08:50
$('#tableId').on('draw.dt', function() {
//This will get called when data table data gets redrawn to the table.
});

- 20,616
- 7
- 42
- 55

- 304
- 2
- 10
-
Apart from the different comment, I don't see how this is different from Alvin's answer.. – Broots Waymb Dec 01 '17 at 17:11
In my case, the 'page.dt' event did not do the trick.
I used 'draw.dt' event instead, and it works!, some code:
$(document).on('draw.dt', function () {
//Do something
});
'Draw.dt' event is fired everytime the datatable page change by searching, ordering or page changing.
/***** Aditional Info *****/
There are some diferences in the way we can declare the event listener. You can asign it to the 'document' or to a 'html object'. The 'document' listeners will always exist in the page and the 'html object' listener will exist only if the object exist in the DOM in the moment of the declaration. Some code:
//Document event listener
$(document).on('draw.dt', function () {
//This will also work with objects loaded by ajax calls
});
//HTML object event listener
$("#some-id").on('draw.dt', function () {
//This will work with existing objects only
});

- 2,941
- 1
- 21
- 18
This working good
$('#id-of-table').on('draw.dt', function() {
// do action here
});

- 13,302
- 8
- 38
- 68

- 71
- 2
An alternative approach would be to register an event handler on the pagination link like so:
$("#dataTableID_paginate").on("click", "a", function() { alert("clicked") });
Replace "#dataTableID_" with the ID of your table, of course. And I'm using JQuery's ON method as that is the current best practice.

- 1,621
- 13
- 19
-
1more specifically, `$('#myTableId_wrapper').find('.dataTables_paginate').on('click', 'li:not(.disabled) a', function() {` – mpen May 17 '14 at 22:28
Try using delegate instead of live as here:
$('#link-wrapper').delegate('a', 'click', function() {
// do something ..
}

- 21
- 2
This works form me to scroll to when I click next
$('#myTable').on('draw.dt', function() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
});

- 21
- 3
-
Thank you for this code snippet, which might provide some limited short-term help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Mar 19 '18 at 17:37
If you handle the draw.dt event after page.dt, you can detect exactly after moving the page. After work, draw.dt must be unbind
$(document).on("page.dt", () => {
$(document).on("draw.dt", changePage);
});
const changePage = () => {
// TODO
$(document).unbind("draw.dt", changePage);
}

- 11
- 1
I've not found anything in the API, but one thing you could do is attach an event handler to the standard paginator and detect if it has changed:
$('.dataTables_length select').live('change', function(){
alert(this.value);
});

- 76,206
- 31
- 145
- 192