views:

39

answers:

2

I have the following html generated through my php code.
What I want to do is that when I select the checkbox in span "row" then the checkboxes for "main_row" span in which "row" is contained in,
should be cheked automatically.
Also when I deselect the "main_row" checkbox, then all the contained in "row: checkboxes should be deselected.
I can use javascript or jquery for this purpose.

<span id="mainTopHeading" >Imports &amp; Exports</span>
<span id="lblmainTopHeading">Categories</span>
<span id="main_row" class="mystyleClass" >
    <input type="checkbox" id="pH" curstomerName="Zubair & CO" recordID="1920"  />
    <span title="Zubair & CO"  id="pHName">Zubair &amp; CO</span>
    <span title="Roland">Roland</span>
</span>
<span id="row"  recordID="1920"  curstomerName="Zubair & CO"   >
    <input type="checkbox"  recordID="1920" curstomerName="Zubair & CO"  />
    <span>Order  Rice &amp; Sugar</span>
    <span>Roland</span>
</span>

<span id="mainTopHeading" >Manufacturing</span>
<span id="lblmainTopHeading">Categories</span>

<span id="main_row" class="mystyleClass" >
    <input type="checkbox" id="pH" curstomerName="Howard Manufacturing" recordID="1870"  />
    <span title="Howard Manufacturing"  id="pHName">Howard Manufacturing</span>
    <span title="Roland"> Roland</span>
</span>
<span id="row" index="1" recordID="1870"  curstomerName="Howard Manufacturing"  >
    <input type="checkbox" style="left:10;" recordID="1870"  curstomerName="Howard Manufacturing" />
    <span>Order Tires and Plastic products</span>
    <span>Roland</span>
</span>
<span id="row" index="1" recordID="1870"  curstomerName="Howard Manufacturing"  >
    <input type="checkbox" style="left:10;" recordID="1870"  curstomerName="Howard Manufacturing" />
    <span>Order Electronics</span>
    <span>Roland</span>
</span>

<span id="main_row" class="mystyleClass"  >
    <input type="checkbox" id="pH" curstomerName="James & Sons" recordID="1866"  />
    <span title="James & Sons"  id="pHName">James &amp; Sons</span>
    <span title="Roland">Roland</span>
</span>
<span id="main_row" class="mystyleClass"  >
    <input type="checkbox" id="pH" curstomerName="Villa Thresa Inc" recordID="1866"  />
    <span title="Villa Thresa Inc"  id="pHName">Villa Thresa Inc</span>
    <span title="Roland">Roland</span>
</span>
<span id="main_row" class="mystyleClass"  >
    <input type="checkbox" id="pH" curstomerName="Bangkok Manufacturing" recordID="1866"  />
    <span title="Bangkok Manufacturing"  id="pHName">Bangkok Manufacturing</span>
    <span title="Roland">Roland</span>
</span>

<span id="row"  recordID="1920"  curstomerName="Zubair & CO"   >
    <input type="checkbox"  recordID="1920" curstomerName="Zubair & CO"  />
    <span>Order  Rice &amp; Sugar</span>
    <span>Roland</span>
</span>

<span id="mainTopHeading">Misc.</span>
<span id="lblmainTopHeading">Different things</span>
<span id="main_row" class="mystyleClass"  >
    <input type="checkbox" id="pH" curstomerName="Bangkok Manufacturing" recordID="1866"  />
    <span title="Bangkok Manufacturing"  id="pHName">Bangkok Manufacturing</span>
    <span title="Roland">Roland</span>
</span>​
+2  A: 

Once you make the id to class change @patrick suggests above, you can do this:

$(".main_row :checkbox").change(function() {
  $(this).closest(".main_row").nextUntil(".main_row")
         .find(":checkbox").attr("checked", this.checked);
});

You can try a demo here, when you check/uncheck the checkbox in the .main_row, it goes up to the .main_row it's in, uses .nextUntil(".main_row") to get all the <span> elements between the clicked .main_row and the next, so this category. Then it gets any checkboxes in there and sets their checked property to the same as the checkbox in the .main_row you clicked on.

If there may be something besides .row in there, you can add a .filter(".row") after the .nextuntil() to restrict it to .row elements.

Nick Craver
I think OP wanted the trigger to be on the `checkbox` under `.row`: *' when I select the checkbox in span "row"'*. So you'd need to backtrack to `.main_row` first. I still think the question implies nesting that doesn't exist. Your example really messed with my head until I looked at the CSS and realized you used `nextUntil()`. :o)
patrick dw
Hi Nick Craver Thanks for that however, its working opposite. What I wanted was when I check the sub checkbox, then it should check the main check box and it there are no sub checkboxes checked, then the main checkbox should not be checked. Can you tell me how. Thanks again
@user295189 - Do you mean like this? http://jsfiddle.net/rzwzF/2/ Still trying to grasp exactly what you're after :)
Nick Craver
Nick Craver, yes almost! only thing is that its not letting me select if there are no sub-checkboxes. I still want to be able to select if there are no sub checkboxes. Thanks a millions
@user295189 - In that case just remove the very first like that sets those to `disabled`, like this: http://jsfiddle.net/rzwzF/3/
Nick Craver
@Nick Craver - if I do that then it will be a bug as the users can uncheck the top level but not the sub one, which is a problem. so if they unselect the top one can we unselect all the bottom ones too?thanks
@user295189 - Combine the first and third demos, like this: http://jsfiddle.net/rzwzF/4/
Nick Craver
+2  A: 

Here's a solution that (I think) does what you want.

(Used Nick's example as a starting point. I see that Nick posted a similar solution, but this one takes a different approach. )

In addition to what you requested, if you click the checkbox under .main_row, it will check/uncheck all its related .row checkboxes.

Try it out: http://jsfiddle.net/nykda/

$(".row input").change(function() {
    var $row= $(this).closest(".row");
    var $main_row = $row.prev('.main_row').length ? $row.prev('.main_row') : $row.prevUntil(".main_row").prev();
    $main_row.find(":checkbox").attr("checked", function(i,attr) {
        return $main_row.nextUntil('.main_row').filter(':has(input:checked)').length ? "checked" : false;
    });
});

$(".main_row input").change(function() {
    $(this).closest(".main_row").nextUntil('.main_row').children(':checkbox').attr('checked', this.checked);
});
patrick dw
+1 - Glad you resolved this, I'm knee-deep in figuring out why someone thought we needed `CookieCollection` *and* `CookieContainer` in .Net :-/
Nick Craver