szerz
szerz

Reputation: 257

How to catch td index in Datatable?

I have datatable and trying to catch column+row index when user press on it:

$('#datatable tbody').on('click', 'tr', function(){
    var eq = $(this).index(); 
    console.log(eq); 

});

So, I could catch index of row (tr) by this way. However, I need also column (td index) with it. Does anyone could advise some approach?

Here is an example of similar task, just from backside (when someone needs few catchers). I am not good enought in jquery to convert it to my trouble.

Upvotes: 1

Views: 1191

Answers (1)

Paweł Kuffel
Paweł Kuffel

Reputation: 471

You can listen for td clicks and then get the indices of both the clicked td and it's parent tr:

$('#datatable tbody').on('click', 'td', function(){
    var clicked_td = $(this);
    var td_index = clicked_td.index(); 
    var tr_index = clicked_td.parent().index();
    console.log(td_index); 
    console.log(tr_index); 
});

Upvotes: 2

Related Questions