I have the following jQuery click functions:
$childCheckbox.click(function(){
if(!$(this).hasClass('checked') && !$parentCheckbox.hasClass('checked')){
$(this).attr('checked', 'checked').addClass('checked');
$parentCheckbox.prop('indeterminate',true);
}
else if($(this).hasClass('checked')){
$(this).removeAttr('checked', 'checked').removeClass('checked');
$parentCheckbox.prop('indeterminate',false);
}
});
$subparentCheckbox.click(function(){
if(!$(this).hasClass('checked') && !$parentCheckbox.hasClass('checked')){
$(this).attr('checked', 'checked').addClass('checked');
$parentCheckbox.prop('indeterminate',true);
}
else if($(this).hasClass('checked')){
$(this).removeAttr('checked', 'checked').removeClass('checked');
$parentCheckbox.prop('indeterminate',false);
}
});
As you can see both are exactly the same with the exception of the "selector". Is there a good way to combine these to minimize code?
$(this)
in a temporary variable to avoid repeated calls to jQuery:var $this = $(this);
\$\endgroup\$