Tuesday, December 27, 2011

Autoselection of checkboxes in a table

The code doing selection
$(function () {
    "use strict";
    var toggleSelect = $("#all"),
        checkboxes = $("#table").find("tbody input[type=checkbox]");
    toggleSelect.click(function () {
        checkboxes.attr('checked', $(this).is(":checked"));
    });
    checkboxes.click(function () {
        toggleSelect.attr('checked', checkboxes.length === checkboxes.filter(':checked').length);
    });
});
A sample table
Username Phone Number
User 1 0123456789
User 2 0133456789

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.