Ali Alhamaly Ali Alhamaly - 1 month ago 7
jQuery Question

Get id value from Datatable button

I have a

datatable
in my project which works perfectly, in the last column I'm adding a button for each row which also works fine.

I'm assigning a specific value to the
id
attribute of this button and trying now to get that value when clicking on the button but I didn't manage to get that

Any suggestions please?

var table = $("#example").DataTable();
$('#example tbody').on('click', 'button', function () {
var id = table.row($(this).attr('id'));
console.log(id);
});


but the above code returns:


[0: Array[0], context: Array[1], selector: Object, ajax: Object]

Answer

If the 'id' attribute assigned to your button use this;

var table = $("#example").DataTable();
$('#example tbody').on('click', 'button', function () {
  var id = $(this).attr('id'); //$(this) refers the clicked button element
  console.log(id);
});