How do I get the dataTable element from within a click handler

How do I get the dataTable element from within a click handler

tacman1123tacman1123 Posts: 181Questions: 41Answers: 1

This is pretty common in the example code:

var table = $('#example').DataTable( {
    columnDefs: [
        { visible: false, targets: 1 }
    ]
} );
 
$('#example tbody').on( 'click', 'td', function () {
    var columnData = table
        .column( $(this).index()+':visIdx' )
        .data();
} );

But often my click handlers work on multiple tables, based on some class. And what I really need is the datatable that contains the element that was clicked on.

Right now I have some hacky code to find the id of the nearest "table" element, and then try to access is via a jQuery selector, but that way is very messy. Admittedly, I'm confused the $this and $(this). And none of the examples on this site pass back the event or other information in the click handler.

An example that got the datatable from within the handler, rather than going to a variable outside the scope, would be much appreciated!

Answers

This discussion has been closed.