dhasan k
dhasan k

Reputation: 169

Check whether checkbox in a jquery datatable row is checked or not

I am new to JQuery and datatables. I have a datatable with three columns in a row. First column is a checkbox and other two columns are text. I want to check whether the first column(checkbox) is checked or not on some event. I am able to get the other fields but i don't know how to check if the checkbox is checked. I have the row id and i am using the below code to get the row and the other columns

function uncheckRow(trId) {<br>
    var table = $(‘#example’).DataTable();
    var row = table.row("#"+trId);
    var rowData = row.data();
    var name = rowData[1];
    var age = rowData[2];
    // need to check if the check box in the 1st column(rowData[0]) is checked or not, If it is checked, i have to uncheck
    …
}

I also tried the below approach, it is working fine. But when i move to next page using pagination, this is not working.

var td = $("#"+trId).find("td");
var chkBox = td.eq(0).find('input');
if(chkBox.is(':checked')){
    chkBox.prop('checked', false);
}

Please provide the solution. Thanks.

Upvotes: 1

Views: 11902

Answers (2)

Urja Satodiya
Urja Satodiya

Reputation: 412

This may work. Link attached below has full demo with jquery datatable.

DataTable with Checkbox demo: https://www.gyrocode.com/articles/jquery-datatables-checkboxes/

Upvotes: 0

Eric Guan
Eric Guan

Reputation: 15982

Try this. You can access the node in memory if you have a reference to the row object, regardless if it's in the DOM or not.

function uncheckRow(trId) {
    var table = $(‘#example’).DataTable();
    var row = table.row("#"+trId);
    var rowData = row.data();
    var name = rowData[1];
    var age = rowData[2];

    var $tr = $(row.node());
    var $checkbox = $tr.find('td:first-child input[type="checkbox"]')
    if($checkbox.is(':checked')){
        $checkbox.prop('checked', false);
    }
}

Upvotes: 3

Related Questions