Reputation: 1810
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);
}
});
});
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);
});
Upvotes: 2
Views: 13310
Reputation: 67187
Try to use .find()
at this context since we are trying to retrieve the td
value from the click event of its parent tr
,
$('#jsontable_salary').on('click', 'tr', function (e) {
e.preventDefault();
var rowIndex = $(this).find('td:eq(0)').text();
alert(rowIndex);
});
Elements that are getting loaded during the run time must use event delegation in order to attach an event with it.
Upvotes: 2
Reputation: 28519
You can try
var rowIndex = $(this).find('td').first().text()
your attempt with closest doesn't work, as closest traverses the DOM upwards
Upvotes: 2