How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Trigger a Function When DataTables Loads Data
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Trigger a Function When DataTables Loads Data
Web Development

How to Trigger a Function When DataTables Loads Data

how7o
By how7o
Last updated: May 23, 2026
5 Min Read
Trigger a function when DataTables loads data via Ajax
SHARE

To run a function every time DataTables finishes loading data via Ajax, listen for the xhr.dt event on the table element. The event fires once per Ajax request and gives you the raw server response, so you can react to load completion or inspect what came back.

Contents
  • The xhr.dt event
  • Handler arguments
  • Post-render — use draw.dt instead
  • Reshape a third-party response inside xhr.dt
  • Bind events to the rendered rows
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 with DataTables 2.0. Originally published 2024-03-21, rewritten and updated 2026-05-17.

The xhr.dt event

$('#data-table')
    .on('xhr.dt', function (e, settings, json, xhr) {
        console.log('Ajax load complete', json);
        // your post-load logic
    })
    .DataTable({
        ajax: '/api/employees',
        columns: [ /* ... */ ],
    });

Bind the handler before calling .DataTable() — DataTables fires xhr.dt internally during initialisation, so a handler added afterwards misses the very first request.

DataTables xhr.dt event — before .DataTable() init, handler args, draw.dt alternative for post-render

Handler arguments

  • e — the jQuery event object.
  • settings — DataTables’ internal settings for this instance. Rarely needed; useful for debugging.
  • json — the raw server response. Mutating it here changes what DataTables ingests, because xhr.dt fires before the rows are parsed.
  • xhr — the underlying jQuery XHR object. Has status, responseText, etc.

Post-render — use draw.dt instead

$('#data-table')
    .on('draw.dt', function () {
        // Runs after rows are rendered, including on pagination/sort/search
        $('#data-table tbody tr').hover(/* ... */);
    })
    .DataTable({ ajax: '/api/employees', columns: [/* ... */] });

xhr.dt fires on Ajax response. draw.dt fires every redraw (Ajax response + pagination + sort + search). Use draw.dt when you need to manipulate the rendered DOM; use xhr.dt when you only care about Ajax responses.

Reshape a third-party response inside xhr.dt

$('#data-table')
    .on('xhr.dt', function (e, settings, json) {
        // Server returns { items: [...] }, DataTables expects { data: [...] }
        if (json && json.items) {
            json.data = json.items;
        }
    })
    .DataTable({ ajax: '/external/api' });

Because xhr.dt fires before DataTables parses the response, you can rewrite the JSON in place — useful when the endpoint shape doesn’t match DataTables’ expected format and you can’t change the server.

Bind events to the rendered rows

// Event delegation — listener on the static , matches rows that appear later
$('#data-table tbody').on('click', '.edit-link', function () {
    const data = $('#data-table').DataTable().row( $(this).closest('tr') ).data();
    console.log('clicked row', data);
});



Avoid binding handlers in draw.dt on a per-row basis — you’ll add the same handler multiple times. Use delegated handlers on a static parent (the <tbody>) so a single listener handles every row across redraws.

Frequently asked questions

What’s the difference between xhr.dt and draw.dt?

xhr.dt fires only on Ajax responses — once per network request, before the table actually renders the new rows. draw.dt fires every time the table redraws, which includes Ajax loads and pagination/sort/search interactions. If you want a post-render hook that runs after every visible change, draw.dt is the broader choice.

Why bind the event before calling .DataTable()?

DataTables fires xhr.dt internally on initialisation when ajax is set, so handlers attached after .DataTable() miss the first event. Binding to the underlying <table> element before the init call ensures you catch every Ajax response including the first.

Is the json argument the raw server response?

Yes — exactly what the server returned. You can inspect json.recordsTotal, json.data, or any custom fields you’ve added. Modifying json here also affects what DataTables ingests, because the event fires before the table reads the response. Useful for response massaging — e.g. reshaping a third-party API into the DataTables format.

How do I attach event handlers to the newly rendered rows?

Use draw.dt (fires after rendering) or, more efficiently, delegate from a parent: $('#data-table tbody').on('click', '.row-action', handler). Delegated handlers survive pagination, redraws, and re-renders because the listener lives on the parent, not on the per-row elements.

Related guides

  • How to Load Data in DataTables Using Ajax
  • How to Reload DataTables with a New Ajax URL
  • How to Create Ajax-Based Pagination in DataTables

References

DataTables xhr.dt event: datatables.net/reference/event/xhr. DataTables draw.dt event: datatables.net/reference/event/draw.

TAGGED:ajaxdatatablesJavaScriptjQuery

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Submit a form with jQuery How to Submit a Form Using jQuery
Next Article Replace jQuery .each with vanilla JavaScript loops How to Replace jQuery’s .each() with Vanilla JavaScript
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

Create custom exception class in Laravel (Artisan command + secure error handling)
Web Development

How to Create a Custom Exception Class in Laravel (With Clean JSON Responses)

6 Min Read
Laravel last inserted ID — Eloquent save populates model primary key illustration
Web Development

How to Retrieve the Last Inserted ID in Laravel Eloquent

8 Min Read
Display only the current date in Laravel using Carbon
Web Development

How to Display Only the Current Date in Laravel (Carbon Examples)

4 Min Read
Laravel Eloquent orderBy — code snippet sorting posts by id descending with arrow icons
Web Development

How to Use orderBy in Laravel Eloquent (with Examples)

6 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?