How to check if atleast one checkbox is checked

I had an issue where I had a list of checkboxes and I needed that after at least one checkbox is clicked to show a div block.

I came up with this:

$(document).ready(function() {
$('input[name=whatever]').change(function() {
  checked = $("input[name=whatever]:checked").length;
  $('#block').hide();
  $('#block2').show();

  if (checked > 0) {
      $('#block').show();
      $('#block2').hide();
  }
});

Code above checks how much there is checked checkboxes and fires the event if at atleast one is checked.

Here is an example in jsfiddle

On Jan 16, 2013 Posted by and it is filed under: Resource

Leave a reply