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>

2
  • 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 Commented Apr 13, 2018 at 4:09
  • Possible duplicate of jQuery see if any or no checkboxes are selected Commented Apr 13, 2018 at 4:18

1 Answer 1

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()" />

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.