SO - 22445240
by Arun P Johny
HTML
<fieldset>
<label>
<input type="checkbox" class="checkall"/><strong> All</strong>
</label>
<fieldset>
<label>
<input type="checkbox" name="checkedLocations" class="chkChild checkall" value="China" />
<strong> China</strong>
</label>
<label>
<input type="checkbox" name="checkedLocations" class="chkChild" value="Hong Kong" checked="checked" />
Hong Kong
</label>
</fieldset>
<fieldset>
<label>
<input type="checkbox" name="checkedLocations" class="chkChild checkall" value="Australia" />
<strong> Australia</strong>
</label>
<label>
<input type="checkbox" name="checkedLocations" class="chkChild" value="NSW" />
NSW
</label>
<label>
<input type="checkbox" name="checkedLocations" class="chkChild" value="VIC" />
VIC
</label>
</fieldset>
</fieldset>
JavaScript
$(function () {
$('.checkall').on('click', function () {
$(this).closest('fieldset').find(':checkbox').prop('checked', this.checked);
});
$('.chkChild').on('click', function () {
if (!$(this).is('.checkall')) {
$(this).closest('fieldset').find('.checkall').prop('checked', false);
}
});
});