Skip to content

Instantly share code, notes, and snippets.

@jerronimo
Created October 24, 2017 20:48
Show Gist options
  • Save jerronimo/3bcdd55334c2d2f056e497f691d7e7b5 to your computer and use it in GitHub Desktop.
Save jerronimo/3bcdd55334c2d2f056e497f691d7e7b5 to your computer and use it in GitHub Desktop.
/**
* @param {string} box
* @param {string} all
* @param {string} item
*/
function checkboxList(box, all, item) {
//select all statuses
$(box).on('change', all, function(){
$(item).prop('checked', $(this).prop('checked'));
});
//".checkbox" change
$(box).on('change', item, function(){
//uncheck "select all", if one of the listed checkbox item is unchecked
if(false == $(this).prop('checked')){ //if this item is unchecked
$(all).prop('checked', false); //change "select all" checked status to false
}
//check "select all" if all checkbox items are checked
if ($(item+':checked').length == $(item).length ){
$(all).prop('checked', true);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment