Question

i have a table where i am filling some data and have checkboxes besides each and the last check box is "ALL". When i check this "ALL" checkbox remaining check boxes should be checked and vice versa.

code for filling data.

<div class="tablecontatiner">

<table>
                <tr>
                    <th>Function</th>
                    <th>Add</th>
                    <th>Edit</th>
                    <th>View</th>
                    <th>Delete</th>
                    <th>All</th>
                    </tr>
                @foreach (var item in Model)
                {
                <tr class="grouprow">
                    <td><input type="hidden"/><input id="@(item.Func_Code)" type="checkbox"  style="visibility:hidden" />@item.FunctionName</td>
                    <td><input id="@(item.Func_Code + "A")" type="checkbox" value="Add" @(item.Add==true?"checked":"") /></td>
                    <td><input id="@(item.Func_Code + "E")" type="checkbox" value="Edit" @(item.Edit==true?"checked":"") /></td>
                    <td><input id="@(item.Func_Code + "V")" type="checkbox" value="View" @(item.View==true?"checked":"")/></td>
                    <td><input id="@(item.Func_Code + "D")" type="checkbox" value="Delete" @(item.Delete==true?"checked":"")/></td>
                    <td><input id="@(item.Func_Code + "ALL")" type="checkbox" value="All"/></td>
                    </tr>
                }
 </table>
</div>

and my UI looks like this:

Was it helpful?

Solution

Try this

$('#checkboxAll').on('change', function () {
    $(this).closest('.grouprow').find(':checkbox').not(this).prop('checked', this.checked);
});
$('table tr input:checkbox:not("#checkboxAll")').on('change', function () {
    if (!this.checked) {
        $('#checkboxAll').prop('checked', false);
    }
});

DEMO

OTHER TIPS

Try

jQuery(function ($) {
    //listen to the change of checkbox in the last td
    $('.tablecontatiner td:last-child input').on('change', function () {
        //update the checked property of all checkboxes in the same row as the changed checkbox
        $(this).closest('tr').find('input[type="checkbox"]').not(this).prop('checked', this.checked);
    })
})

Try,

$('#chkAll').on('click', function () { $(this).closest('.grouprow').find(':checkbox').not(this).prop('checked', this.checked); });

Anton's solution is very elegant. If you load (or reload) the table and don't want to set the event handlers each time it is reloaded, then I would change a bit the solution:

$('.tablecontatiner').on('change', '[id$=All]', function () {
    $(this).closest('.grouprow').find('input').not(this).prop('checked', this.checked);
});

Remark: Also I would give "checkboxAll" class to each "ALL" checkbox, so it will look like:

<input id="@(item.Func_Code + "ALL")" type="checkbox" value="All" class="checkboxAll"/>

and then the code above will be:

$('.tablecontatiner').on('change', '.checkboxAll', function () {
    $(this).closest('.grouprow').find('input').not(this).prop('checked', this.checked);
});

http://jsbin.com/jetewayi/1/edit

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top