I have nested unordered lists, with a list item as it's heading that looks something like this.
<ul>
<li><input type="checkbox" /> Header 1</li>
<ul>
<li><input type="checkbox" value="1" />Item 1</li>
</ul>
<li><input type="checkbox" /> Header 2</li>
<ul>
<li><input type="checkbox" value="1" />Item 1</li>
<li><input type="checkbox" value="2" />Item 2</li>
<li><input type="checkbox" value="3" />Item 3</li>
<li><input type="checkbox" value="4" />Item 4</li>
</ul>
<li><input type="checkbox" /> Header 3</li>
<ul>
<li><input type="checkbox" value="5" />Item 5</li>
<li><input type="checkbox" value="6" />Item 6</li>
<li><input type="checkbox" value="7" />Item 7</li>
<li><input type="checkbox" value="8" />Item 8</li>
<li><input type="checkbox" value="9" />Item 9</li>
<li><input type="checkbox" value="10" />Item 10</li>
<li><input type="checkbox" value="11" />Item 11</li>
<li><input type="checkbox" value="12" />Item 12</li>
<li><input type="checkbox" value="13" />Item 13</li>
<li><input type="checkbox" value="14" />Item 14</li>
<li><input type="checkbox" value="15" />Item 15</li>
<li><input type="checkbox" value="16" />Item 16</li>
</ul>
</ul>
Notice that Item 1 is found in Header 1's list, and Header 2's list.
There are a few things that I am trying to accomplish.
- When I click on an header item, it will automatically select or de-select all list items
li
within it'sul
list immediately following it. - When I click a list item
li
within an nestedul
I want to check to see if the other items are all checked (in this case, place a check mark on the Header List Item), or unchecked (in this case, remove the check mark on the Header List Item) if it's a mix of checked and unchecked then set the Header List Item to Indeterminate state. - When I check a List Item, such as List Item 1 that shows up in Header 1's list and Header 2's list, I want it to uncheck both items, or check both items in both lists, and cause the check in requirement two to take place on the effected lists.
I'm having a really hard time of doing this because I'm not really all that good at JavaScript and all of the code I've read thus far does not really come close to what I need.
Any help would be appreciated. I am using jQuery 1.9.1, but if you want to do it in bare bones JavaScript that is fine with me as well (as it's easier to follow when you set a brake point in Google Dev Tools to see what is going on).