As continuation of my previous question dealing with checkboxes in MVC. I've been doing just like this from this question but it seems that it's not working because of the hidden field created by the CheckBoxFor
of MVC. Any idea why? Here's my fiddle showing the actual html
My actual html from CheckboxFor:
<form role="form">
<ul class="col-xs-3">
<li class="checkbox checkbox-info">
<input id="1" name="[0].IsSelected" type="checkbox" value="true"><input name="[0].IsSelected" type="hidden" value="false">
<label for="1"><b>Faa</b></label>
</li>
<li class="checkbox checkbox-info col-xs-offset-1">
<input id="2" name="[0].SubsCategories[0].IsSelected" type="checkbox" value="true"><input name="[0].SubsCategories[0].IsSelected" type="hidden" value="false">
<label for="2">Faa1</label>
</li>
</ul>
<ul class="col-xs-3">
<li class="checkbox checkbox-info">
<input id="3" name="[1].IsSelected" type="checkbox" value="true"><input name="[1].IsSelected" type="hidden" value="false">
<label for="3"><b>Fee</b></label>
</li>
<li class="checkbox checkbox-info col-xs-offset-1">
<input id="4" name="[1].SubsCategories[0].IsSelected" type="checkbox" value="true"><input name="[1].SubsCategories[0].IsSelected" type="hidden" value="false">
<label for="4">Fee1</label>
</li>
<li class="checkbox checkbox-info col-xs-offset-1">
<input id="5" name="[1].SubsCategories[1].IsSelected" type="checkbox" value="true"><input name="[1].SubsCategories[1].IsSelected" type="hidden" value="false">
<label for="5">Fee2</label>
</li>
</ul>
<ul class="col-xs-3">
<li class="checkbox checkbox-info">
<input id="6" name="[2].IsSelected" type="checkbox" value="true"><input name="[2].IsSelected" type="hidden" value="false">
<label for="6"><b>Fii</b></label>
</li>
<li class="checkbox checkbox-info col-xs-offset-1">
<input id="7" name="[2].SubsCategories[0].IsSelected" type="checkbox" value="true"><input name="[2].SubsCategories[0].IsSelected" type="hidden" value="false">
<label for="7">Fii1</label>
</li>
<li class="checkbox checkbox-info col-xs-offset-1">
<input id="8" name="[2].SubsCategories[1].IsSelected" type="checkbox" value="true"><input name="[2].SubsCategories[1].IsSelected" type="hidden" value="false">
<label for="8">Fii2</label>
</li>
</ul>
<ul class="col-xs-3">
<li class="checkbox checkbox-info">
<input id="9" name="[3].IsSelected" type="checkbox" value="true"><input name="[3].IsSelected" type="hidden" value="false">
<label for="9"><b>Foo</b></label>
</li>
<li class="checkbox checkbox-info col-xs-offset-1">
<input id="10" name="[3].SubsCategories[0].IsSelected" type="checkbox" value="true"><input name="[3].SubsCategories[0].IsSelected" type="hidden" value="false">
<label for="10">Foo1</label>
</li>
</ul>
<ul class="col-xs-3">
<li class="checkbox checkbox-info">
<input id="11" name="[4].IsSelected" type="checkbox" value="true"><input name="[4].IsSelected" type="hidden" value="false">
<label for="11"><b>Fuu</b></label>
</li>
</ul>
<ul class="col-xs-3">
<li class="checkbox checkbox-info">
<input id="12" name="[5].IsSelected" type="checkbox" value="true"><input name="[5].IsSelected" type="hidden" value="false">
<label for="12"><b>Bee</b></label>
</li>
</ul>
<ul class="col-xs-3">
<li class="checkbox checkbox-info">
<input id="13" name="[6].IsSelected" type="checkbox" value="true"><input name="[6].IsSelected" type="hidden" value="false">
<label for="13"><b>Bii</b></label>
</li>
<li class="checkbox checkbox-info col-xs-offset-1">
<input id="14" name="[6].SubsCategories[0].IsSelected" type="checkbox" value="true"><input name="[6].SubsCategories[0].IsSelected" type="hidden" value="false">
<label for="14">Bii1</label>
</li>
</ul>
</form>
I'm trying to check the header and all the child checkboxes will be checked and if all the child checkboxes are being check, the parent checkbox will be checked and vice versa.
The js I'm trying to work on:
$('li :checkbox').on('click', function () {
var $chk = $(this),
$li = $chk.closest('li'),
$ul, $parent;
if ($li.has('ul')) {
$li.find(':checkbox').not(this).prop('checked', this.checked)
}
do {
$ul = $li.parent();
$parent = $ul.siblings(':checkbox');
if ($chk.is(':checked')) {
$parent.prop('checked', $ul.has(':checkbox:not(:checked)').length == 0)
} else {
$parent.prop('checked', false)
}
$chk = $parent;
$li = $chk.closest('li');
} while ($ul.is(':not(.someclass)'));
});
UPDATE:
I made it working with the help of the answers below. Here's the working answer in fiddle. I did follow the suggestions by putting the flag whether the checkbox is parent or not. For safety, I also added child-li
class so that I'll not use the bootstrap col-xs-offset-1
, that might change anytime. Thank you very much!