Sachi Tekina Sachi Tekina - 29 days ago 4
jQuery Question

How to get values of the selected table cell from datatable?

This my Jquery:

var oTable_salary = $('#jsontable_salary').dataTable(); //Initialize the datatable
$("#btn_ca_salary").click(function(){
$.ajax({
url: 'proc_php/get_salary.php',
dataType: 'json',
success: function(s){
oTable_salary.fnClearTable();
for(var i = 0; i < s.length; i++) {
oTable_salary.fnAddData([
s[i][0],
s[i][1],
s[i][2],
s[i][3]
]);
} // End For
},
error: function(e){
alert(e.responseText);
}
});
});


enter image description here

I want to get the id from the datable when it is selected. I came up with this but it doesn't work:

$('#jsontable_salary tbody tr').on('click', function (e) {
e.preventDefault();
var rowIndex = $(this).closest('td')[0].text;
alert(rowIndex);
});

Answer

You can try

var rowIndex =  $(this).find('td').first().text()

your attempt with closest doesn't work, as closest traverses the DOM upwards

Comments