Skip to content

Instantly share code, notes, and snippets.

@UmeshSingla
Created June 9, 2013 13:10
Show Gist options
  • Save UmeshSingla/5743464 to your computer and use it in GitHub Desktop.
Save UmeshSingla/5743464 to your computer and use it in GitHub Desktop.
Select all Checkboxes, Unselect all Checkboxes
jQuery('body').on('click','.topic-checkbox input[type="checkbox"]', function(){
var current_checkbox = jQuery(this);
var id = current_checkbox.attr('id');
var checked = current_checkbox.prop('checked');
if(id=="checkbox-input-all" && checked){
jQuery('.topic-checkbox input[type="checkbox"]').prop('checked', true);
}else if(id=="checkbox-input-all" && !checked){
jQuery('.topic-checkbox input[type="checkbox"]').prop('checked', false);
}else if(id!="checkbox-input-all" && !checked){
jQuery('#checkbox-input-all').prop('checked', false);
}else if( jQuery('.checkbox:checked').length == jQuery('.checkbox').length) {
jQuery('.topic-checkbox input[type="checkbox"]').prop('checked', true);
}
});
<ul class="topic-list">
<li class="topic-checkbox" id="all_topics">
<span><input type="checkbox" name="slected_topics[]" id="checkbox-input-all" value="all"/></span>
<span class="checkbox-label"><label for="checkbox-input-all">Select All</label></span>
</li>
<li class="topic-checkbox">
<span><input type="checkbox" name="slected_topics[]" class="checkbox" id="checkbox-input-0" value="4"/></span>
<span class="checkbox-label"><label for="checkbox-input0">Animals</label></span>
</li>
<li class="topic-checkbox">
<span><input type="checkbox" name="slected_topics[]" class="checkbox" id="checkbox-input-1" value="5"/></span>
<span class="checkbox-label"><label for="checkbox-input1">Art</label></span>
</li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment