hptech
hptech

Reputation: 5

Auto-select child checkboxes

I've to work with the roles of the users, that I have in a sql base. Every role has a checkbox, the idea is to check the "childrens" checkboxes if the "parent" is checked, and check the "parent" if all the "childrens" are checked.

I tried this script in a page and it worked, but I can not make this work in my asp.net

This is my script and code.

$(document).ready(function () {
    $('.parent').click(function () {
        if ($(this).is(':checked'))
            $(this).parent().find('.children input[type="checkbox"]').attr('checked', 'checked');
    });

    $('.children input[type="checkbox"]').click(function () {
        var numberOfCheckboxes = $(this).parents('.children').find('input[type="checkbox"]').length;

        if ($(this).parents('.children').find('input[type="checkbox"]').filter('input[checked=checked]').length == numberOfCheckboxes)
            $(this).parents('div').find('.parent').attr('checked', 'checked');
    });
});
<div>
    <asp:DataList ID="outerDataList" runat="server" DataSourceID="ObjectDataSource2" >
        <ItemTemplate>
            <li>
                <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>'
                    Visible="False" />
                <asp:Label ID="typeLabel" runat="server" Text='<%# Eval("type") %>'
                    Visible="False" />
                <asp:CheckBox ID="chkEnabled" class="parent" runat="server"
                    Checked='<%# Eval("Enabled") %>' Text='<%# Eval("name") %>' />
            </li>
            <ul class="children">
                <asp:DataList ID="innerDataList" runat="server" DataSource='<%# Eval("Roles") %>'>
                    <ItemTemplate>                                
                        <li>
                            <asp:Label ID="idLabelInt" runat="server"
                                Text='<%# Eval("id") %>' Visible="False" />
                            <asp:Label ID="typeLabelInt" runat="server"
                                Text='<%# Eval("type") %>' Visible="False" />
                            <asp:CheckBox ID="chkEnabledInt" runat="server"
                                Checked='<%# Eval("Enabled") %>' Text='<%# Eval("name") %>' />
                        </li>
                    </ItemTemplate>
                </asp:DataList> 
            </ul>                   
        </ItemTemplate>
    </asp:DataList>
</div>

Upvotes: 0

Views: 814

Answers (1)

gilly3
gilly3

Reputation: 91587

Move your list item closing tag to encapsulate the sub-list.

<div>
    <asp:DataList ID="outerDataList" runat="server" DataSourceID="ObjectDataSource2" >
        <ItemTemplate>
            <li>
                <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>'
                    Visible="False" />
                <asp:Label ID="typeLabel" runat="server" Text='<%# Eval("type") %>'
                    Visible="False" />
                <asp:CheckBox ID="chkEnabled" class="parent" runat="server"
                    Checked='<%# Eval("Enabled") %>' Text='<%# Eval("name") %>' />
                <ul class="children">
                    <asp:DataList ID="innerDataList" runat="server" DataSource='<%# Eval("Roles") %>'>
                        <ItemTemplate>
                            <li>
                                <asp:Label ID="idLabelInt" runat="server"
                                    Text='<%# Eval("id") %>' Visible="False" />
                                <asp:Label ID="typeLabelInt" runat="server"
                                    Text='<%# Eval("type") %>' Visible="False" />
                                <asp:CheckBox ID="chkEnabledInt" runat="server"
                                    Checked='<%# Eval("Enabled") %>' Text='<%# Eval("name") %>' />
                            </li>
                        </ItemTemplate>
                    </asp:DataList>
                </ul>
            </li>
        </ItemTemplate>
    </asp:DataList>
</div>

Here, you are looking for descendants of the checkbox's parent with a class of children:

$(this).parent().find('.children input[type="checkbox"]')

As you currently have it, with the <ul> as a sibling of the <li>, rather than a child, this selector doesn't work.

Besides it breaking your selector, it's also invalid HTML. A <li> must be a child of a <ul>, <ol>, or <menu> element. The only allowed children of these elements are <li> elements. (The exception being <menu>, which can alternatively contain flow content instead of <li> elements.) To generate valid HTML, you must replace your parent <div> element with a <ul>, <ol>, or <menu>.

Also, I think you want .closest(), which selects a single element - the first ancestor matching the selector, instead of .parents(), which selects every ancestor matching the selector.

Upvotes: 1

Related Questions