Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive



Need one checkbox to check/uncheck other checboxes or even the checkboxes that are generated by the .NET CheckBoxList ?

Just include this piece of javascript on your page and add the classes to your html.

HTML:
<p class="checkbox-master-slave-container">
    <asp:CheckBox runat="server" Text="Select all" CssClass="checkbox-master" />
    <asp:CheckBox runat="server" Text="Option one" CssClass="checkbox-slave" />
    <asp:CheckBox runat="server" Text="Option two" CssClass="checkbox-slave" />
</p>

JavaScript:
function initCheckboxMasterSlave() {
    $('.checkbox-master').live('click', function() {
        // Check or uncheck all slave checkboxes
        $('.checkbox-slave input:checkbox', $(this).parents('.checkbox-master-slave-container'))
            .attr('checked', $('input:checkbox', this).attr('checked'));
    });
    $('.checkbox-slave').live('click', function() {
        // See if all slaves are checked
        var allSlavesChecked =
            ($('.checkbox-slave input:checkbox:checked', $(this).parents('.checkbox-master-slave-container')).length
            == $('.checkbox-slave input:checkbox', $(this).parents('.checkbox-master-slave-container')).length);
        // Check/uncheck the master checkbox
        $('.checkbox-master input:checkbox', $(this).parents('.checkbox-master-slave-container'))
            .attr('checked', allSlavesChecked);
    });
}

 © Evident Interactive BV