user576838
user576838

Reputation: 875

jquery Link click in Datatables not working

I'm using the jquery datatables plugin and I have two links in the first row. Those links are used to send a ajax on click. Since I implemented datatables (I had just a table beforehand) it stopped working. I've looked around and tried two things:

I originally had

               $(".approveReject").click(function () {
                   OnApproveRejectClick("voavi", this);
               });

But replaced that with

$(document).delegate("click", '.approveReject', function (event) {
alert("clicked");
});

with no success, so I tried to add the fnInitComplete callback to the datatable init object:

        "fnInitComplete": function () {
            $(".approveReject").click(function () {
                OnApproveRejectClick("voavi", this);
            });
        }

still nothing. The click doesn't work at all. Any idea what I need to do in order to bind the click event to my links? Thanks

Full datatable init

    $("#voaviTable").dataTable({
        "bJQueryUI": true,
        "bScrollInfinite": true,
        "bScrollCollapse": true,
        "iDisplayLength": 30,
        "sScrollY": "450px",
        "oLanguage": {
            "sSearch": "Filter: "
        },
        "aaSorting": [],
        "fnInitComplete": function () {
            $(".approveReject").click(function () {
                OnApproveRejectClick("voavi", this);
            });
        }
    });

table example row:

<tr class="even">
<td class=" ">
<a id="lnkApprove" class="approveReject" href="#">Approve</a>
|
<a id="lnkReject" class="approveReject" href="#">Reject</a>
<span class="ui-icon ui-icon-circle-check" style="display: none;"></span>
<span class="ui-icon ui-icon-circle-close" style="display: none;"></span>
<img id="loaderGif" height="16px" style="display: none;" src="../../Content/images/loader.gif">
</td>
<td class="statusID "> 32 </td>
<td class="statusText "> new </td>
<td class=" "> </td>
<td class=" "> </td>
<td class=" "> Cote de Blancs </td>
<td class=" "> </td>
<td class=" "> </td>
<td class=" ">
<td class=" "> 10/5/2012 2:54:05 PM </td>
</tr>

Upvotes: 1

Views: 5069

Answers (1)

wirey00
wirey00

Reputation: 33661

You are using delegate wrong

$(document).delegate( '.approveReject', "click",function (event) {// <-- notice where the selector and event is
    alert("clicked");
});

Though if you are using jQuery 1.7+ use .on()

$(document).on("click", '.approveReject', function (event) {
    alert("clicked");
});

best thing would to bind the event to your table though as it's the closest static parent element (I'm guessing)

$('#voaviTable').on('click','.approveReject', function (event) {

$(document).delegate(selector, events, data, handler); // jQuery 1.4.3+

$(document).on(events, selector, data, handler); // jQuery 1.7+

Upvotes: 4

Related Questions