1

I have one table.Inside table I have two <td>.First <td> has one checkbox and second <td> has sections which also contains checkboxes.Onclick of checkbox of first <td>,it should check all the checkboxes of other <td>.This is the code which is using datatable.

for (var i = 0; i < tmpColumns.length; i++) {
            var obj = new Object();
            var table = "";
            obj.title = tmpColumns[i].toUpperCase();
            obj.data = tmpColumns[i];
            obj.defaultContent = "";
            columns.push(obj);
            if(type == "table" && i == 0) {
                obj.render = function (data, type, row, meta) {
                    table = row.name;
                    select = '<section> <label class="checkbox" style="padding-left: 1em;"><input type="checkbox" id = "checkAll" " data-table-name="' + table + '">'  + table + ' <i></i></label></section>';
                    return select;

                }
            }

            if(type == "table" && i == 1) {
                obj.render = function (data, type, row, meta) {
                    table = row.name;

                    for (var j = 0; j < data.length; j++) {
                        var column = table + "." + data[j];
                        ele = ele + '<section> <label class="checkbox" style="padding-left: 1em;"><input type="checkbox" class="addToQuery" data-col="' + column + '" data-table="' + table + '"><i></i>' + data[j] + '</label></section>';
                    }
                    return ele;
                };
                break;
            }
        }

This is the code for checking checkbox after clicking first checkbox.

$("#tableList").on("change",'input[data-table-name]', function () {

        var bool = $(this).prop('checked');     
        var tableName = $(this).attr("data-table-name");
        $('input[data-table="' + tableName + '"]').prop("checked", bool);
    });

I want to check the particular checkbox in which is in the same <tr>.But it is checking all the <tr> elements Thanks.

1
  • 2
    $(this).closest('tr').siblings().find('input[data-table="' + tableName + '"]').prop("checked", bool); Bettter to post generated markup too. Commented Jun 14, 2016 at 5:12

1 Answer 1

1
$("#tableList").on("change",'input[data-table-name]', function () {   
    $(this).closest("tr").find('input[data-table="' + tableName + '"]').prop("checked", $(this).prop('checked'));
});
Sign up to request clarification or add additional context in comments.

Comments

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.