0

I have checkboxes that have an array name checker[] and I want my function to tell me, when unchecking any box, if any of the checkboxes with that name are checked. Can't figure it out.

function doStuff() {
  if (document.forms.theForm.elements.checker[].checked == false)
   alert('none checked');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form name="theForm">
<input type="checkbox" name="checker[]" value="1" onclick="doStuff()"> 
<input type="checkbox" name="checker[]" value="2" onclick="doStuff()">
</form>
Daniel Williams
  • 2,195
  • 7
  • 32
  • 53
  • `Can't figure it out.` Post what you *have* tried for debugging, else this is liable to be closed for a `write-my-code-for-me-from-scratch` question – CertainPerformance Apr 13 '18 at 04:09
  • Possible duplicate of [jQuery see if any or no checkboxes are selected](https://stackoverflow.com/questions/4086957/jquery-see-if-any-or-no-checkboxes-are-selected) – Rafael Herscovici Apr 13 '18 at 04:18

1 Answers1

2

You can check the length property of checked Check Boxes:

$('input[name="checker[]"]:checked').length

function doStuff() {
  var len = $('input[name="checker[]"]:checked').length;
  if (len === 0 )
   alert('none checked');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="checker[]" value="1" onclick="doStuff()" /> 
<input type="checkbox" name="checker[]" value="2" onclick="doStuff()" />
Mamun
  • 66,969
  • 9
  • 47
  • 59