binary binary - 4 months ago 34
Javascript Question

How to get Id value by clicking Edit button on the same row of DataTable

I use

jQuery Datatable
for listing records and add an
button (Edit) for editing the record on a modal dialog. If I select a row I can get the row id value and open the related record on modal dialog. However, if I click the
button directly, I cannot get the Id value of the related record (on the same row) because it is not selected first when clicking
button. What I want to do is that: I want to get the Id value of the row on which I click the Edit button. Is it possible? If not, can I select the hovered row programmatically when I click the
button? (If the prior scenario is possible I would prefer it). Any idea?

function openModal() {

var table = $('#dtbListAccount').DataTable();
var oRow = $('this').parents('tr')[0];
var oData = table.fnGetData(oRow);

//code omitted for brevity

enter image description here


You can use

var table;
$(document).ready( function () {
 table  = $('#example').DataTable();
} );

$('body').on('click', '#btnEdit', function(){
    //to get currently clicked row object
    var row  = $(this).parents('tr')[0];

    //for row data
    console.log( table.row( row ).data() );


Live Demo Here

Use the browser console to see the results.