views:

61

answers:

3

I have the following

<ul>
   <li>Main
    <ul>
        <li><input type="checkbox" onclick="$.SelectChildren(this);" /> parent 1
           <ul>
               <li><input type="checkbox" onclick="$.SelectChildren(this);" />sub 1</li>
               <li><input type="checkbox" onclick="$.SelectChildren(this);" />sub 2
                  <ul>
                     <li><input type="checkbox" onclick="$.SelectChildren(this);" />sub sub 2</li>
                     <li><input type="checkbox" onclick="$.SelectChildren(this);" />sub sub 3</li>
                  </ul>
                </li>
            </ul> 
        </li>
        <li><input type="checkbox" onclick="$.SelectChildren(this);" />parent 2</li>
    </ul>
   </li>
</ul>

Now for my JQuery function:

(function ($) {
    $.SelectChildren = function (element) {
        if ($(element).is(':checked')) {
            $(element).parent().find(':checkbox').attr('checked', true);
        }
        else {
            $(element).parent().find(':checkbox').attr('checked', false);
        }
    }
})(jQuery);

how would I unselect ONLY (all)parent checkboxes when un-checking a child

for example: when clicked "sub sub 2" checkbox -> it should uncheck "sub 2" and "parent 1" checkboxes as well.

thanks

A: 

How about:

$(element).parents(":checkbox").attr('checked', false);

UPDATE. This is wrong, see Nick's solution.

mkoistinen
A checkbox has no children ;)
Nick Craver
No, but they have parents still.
mkoistinen
@mkoistinen - Your selector is finding parent/ancestor elements that *are* checkboxes.
Nick Craver
@Nick, exactly. The OP is asking "how would I unselect ONLY (all)parent checkboxes when un-checking a child". I'm not exactly sure what question you are answering.
mkoistinen
@mkoistinen - The parent check boxes are in `<li>` elements, which *are* parents, you need to get them, then find checkboxes immediately in *them* (as to not get other descendants). Your answer/selector looks for checkbox **parents**, e.g. `<input type="checkbox">child elements here</input>`....this is never valid markup, checkboxes can't have children, so they can't *be* parents.
Nick Craver
@Nick. Oops! You are correct, sir.
mkoistinen
+2  A: 

Use .children() (to get immediate children) instead of .find() (which looks everywhere below) on the .parents() , for example:

(function ($) {
  $.SelectChildren = function (element) {
    $(element).parents().children(':checkbox').attr('checked', element.checked);
  };
})(jQuery);

Since you're passing a boolean, you can just use the .checked property directly as well.

You can give it a try here, the more traditional approach to get the same effect would look like this:

$(function() {
  $(":checkbox").change(function () {
    $(this).parents().children(':checkbox').attr('checked', this.checked);
  });
});

You can give that version a try here.

Nick Craver
A: 

Identify the parent will a specific class like "parent"

Then

$(".parent").attr("checked", false);

Very Simple

mrNepal