peace_love
peace_love

Reputation: 6471

How can I make my event work on all datatable pages?

When click on .mytext after my datatable is loaded, then I get an alert. But this is only working on the first page of the datatable pagination. If I go to another page it is not working anymore.

   var table = $('#table').DataTable({
     "ajax": {
            "url": "data/table.json",
            "dataSrc": "",
        },

      "columns": [
            {
                "data": "id"
            },
            {
                "data": "name"
            },
            {
                "data": "age"
            }
        ],
       initComplete: function () {
          $(".mytext").on("click", function () {
             alert("something happens");
          });
      },
    });

Upvotes: 0

Views: 23

Answers (1)

ADreNaLiNe-DJ
ADreNaLiNe-DJ

Reputation: 4919

You should use event delegation (http://api.jquery.com/on/). Instead of binding directly on the element, you bind on document or a "parent element" and you set the target.

Here is an example:

$(document).on("click", ".mytext", function () {
    alert("something happens");
});

Upvotes: 2

Related Questions