Parent and child checkboxes
<pre><code>  &lt;div id="customerServices"&gt;
        &lt;input id="s9" type="checkbox" /&gt; Parent element&lt;br/&gt;
            &lt;input id="s15" class="parent-s9" type="checkbox" /&gt; Child element&lt;br/&gt;
            &lt;input id="s16" class="parent-s9" type="checkbox" /&gt; Child element&lt;br/&gt;
        &lt;br/&gt;&lt;br/&gt;&lt;br/&gt;

         &lt;input id="s10" type="checkbox" /&gt; Parent element2&lt;br/&gt;
            &lt;input id="s151" class="parent-s10" type="checkbox" /&gt; Child element2&lt;br/&gt;
            &lt;input id="s161" class="parent-s10" type="checkbox" /&gt; Child element2&lt;br/&gt;
          &lt;br/&gt;&lt;br/&gt;&lt;br/&gt;

           &lt;input id="s101" type="checkbox" /&gt; Parent element3&lt;br/&gt;
           &lt;input id="s102" type="checkbox" /&gt; Parent element4&lt;br/&gt;
     &lt;/div&gt;
</code></pre>

<p>There are some checkboxes on a page. I have need:</p>

<ol>
<li>Check all  childs  if  parent is checked.</li>
<li>Uncheck parent if all childs are unchecked.</li>
<li>Check parent if at least one child is checked.</li>
</ol>

<p>This is some some code, but it's doesn't work</p>

<pre><code>  $('input[type=checkbox]').change(function() {
        var id = $(this).attr('id');
        var children = $('.parent-' + id);


        //Is this a child
        if (children.length)
        {
           //Is it checked
             if ($(this).is(':checked'))
             {
                 //Find the parent
                 var className = $(this).attr('class');
                 var pId = className.replace("parent-","");

                 //If the parent is not checked, then check this parent
                 if (!$(pId).is(':checked'))
                     $(pId).attr('checked','checked');
             }

            //Is it NOT checked
            else{
                //Do other childs are not checked too?
               //var className2 = $(this).attr('class');
                //$(className2)
            }
        }
        //If this is a parent, then check all childs
        esle{ 
            children.attr('checked', $(this).attr('checked'));

        }


     });
</code></pre>
