1

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.

1
  • can you add html sample ? Commented Jan 4, 2017 at 3:36

2 Answers 2

3

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);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Eric. For some reason $checkbox.is(':checked') always return 'false' even though the check box is checked.
I would look at $checkbox in the browser dev tools and see if it's actually a checkbox.
Hi Eric, this code is working as expected. var row = table.row("#"+trId).node(); var chkBox = $(row.cells[0]).find('input[type="checkbox"]'); if(chkBox.is(':checked')){ chkBox.prop('checked', false); }
0

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

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

2 Comments

While this link may provide an answer, it is better to summarize the main points in your post, as links can become non-operational.
Sure @adrian-mole, Will summarise the answer with main point. Thanks for the good suggestion. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.