Is it possible to get the data table object reference from a child element

Is it possible to get the data table object reference from a child element

cj1005cj1005 Posts: 142Questions: 45Answers: 1

Hi,

I have multiple tables on a page, these tables were initialized as data tables by class, not ID.

So, when a user clicks a row on any of the data tables I want to detect which table they clicked, before applying any changes is this possible? If so, how?

My current code :

$(document).ready(function () {
InitDataTables();
});

function InitDataTables() {
$(".dtTableSearch").DataTable({
});

$(".dtTableSearch tbody").on("click", "tr", function () {
if ($(this).hasClass("selected")) {
$(this).removeClass("selected");
} else {
<<What should I put here to select the parent table??>>.$("tr.selected").removeClass("selected");
$(this).addClass("selected");
}
});
}

I'm open to suggestions if anyone has a better way of doing this?
Many thanks,

Chris

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,171Questions: 26Answers: 4,922
    Answer ✓

    Something like this should work:
    var table = $(this).closest('table');

    Or this to get the Datatable API:
    var table = $(this).closest('table').DataTable();

    Kevin

  • cj1005cj1005 Posts: 142Questions: 45Answers: 1

    Perfect :smiley: Thank you Kevin

This discussion has been closed.