Laurence Tuck
Laurence Tuck

Reputation: 420

Check if siblings in an unordered list of inputs are checked using jquery

I have an unordered list of checkbox inputs:

<ul>
  <li><input type="checkbox">parent
    <ul>
      <li><input type="checkbox">child
        <ul>
          <li><input type="checkbox">grandchild
            <ul>
              <li><input type="checkbox" checked="checked">great grandchild</li>
              <li><input type="checkbox">great grandchild</li>
              <li><input type="checkbox">great grandchild</li>
              <li><input type="checkbox" checked="checked">great grandchild</li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </li>
  </ul>

If I uncheck one of the checkboxes, how can I check if any siblings are also checked only from that <ul>?

I've tried a number of things such as siblings(), find() and index().

Upvotes: 1

Views: 2053

Answers (1)

James Montagne
James Montagne

Reputation: 78690

$(this).parent().siblings().children(":checkbox:checked").length

This should get you the number of "sibling" checkboxes which are checked.

Upvotes: 2

Related Questions