views:

561

answers:

6

Is there a quick way or function that would tell me true/false if all check boxes are deselected? Without going through array? (with JS and HTML)

All my check boxes have the same name...

<form action="/cgi-bin/Lib.exe" method=POST name="checks" ID="Form2">
    <input type=checkbox name="us" value="Joe" ID="Checkbox1">
    <input type=checkbox name="us" value="Dan" ID="Checkbox2">
    <input type=checkbox name="us" value="Sal" ID="Checkbox3">
</form>
+1  A: 

In summary, this snipped will return true if all are NOT checked. It bails out as soon as a checked one is found.

var a = document.getElementsByName("us");
for(var i=0; i<a.length; i++)
   if(a[i].checked)
      return false;
return true;

(did not test, but conceptually it is valid)

gahooa
getElementsByName is quirky in IE: http://www.quirksmode.org/dom/w3c_core.html
Paolo Bergantino
Thanks for the link - not seen that chart before
Russ Cam
+4  A: 

You have to loop through them. Even a library like jQuery will loop through them, just hide it from you.

var form = document.getElementById('Form2');
var inputs = form.getElementsByTagName('input');
var is_checked = false;
for(var x = 0; x < inputs.length; x++) {
    if(inputs[x].type == 'checkbox' && inputs[x].name == 'us') {
        is_checked = inputs[x].checked;
        if(is_checked) break;
    }
}
// is_checked will be boolean 'true' if any are checked at this point.
Paolo Bergantino
Won't that just return if the last checkbox is checked? Eg. Sal in the example.
svinto
Whoops! Correcto. Fixed.
Paolo Bergantino
+6  A: 

jQuery would be a mass of unneeded bloat for a task this trivial. Consider using it if you are running it for other purposes, but all you need is something like this:

function AreAnyCheckboxesChecked () {
  var checkboxes = document.forms.Form2.elements.us;
  for (var i = 0; i < checkboxes.length; i++) {
    if (checkboxes[i].checked) {
      return true;
    }
  }
  return false;
}
David Dorward
A: 

What do you mean by

Without going through array

?

You could just do

 function check() {
    var anyChecked = false;
    var form = document.getElementById('Form2');
    var checkboxes = form.getElementsByTagName('input');
    for(var i=0; i < checkboxes.length; i++) {
            if (checkboxes[i].checked) {
                anyChecked  = true;
                break;
            }
    } 
    alert("Checkboxes checked? " + anyChecked);
}

Working Demo

Russ Cam
getElementsByName is quirky in IE and a bit in Opera: http://www.quirksmode.org/dom/w3c_core.html, I try to just avoid it altogether.
Paolo Bergantino
cheers - updated now
Russ Cam
+2  A: 

JavaScript:

var allischecked = (function(){
  var o = document.getElementById("Form2").getElementsByTagName("input");
  for(var i=0,l=o.length;i<l;i++){
    o[i].type === "checkbox" && o[i].name === "us" && o[i].checked || return false;
  }
  return true;
})();

With jQuery:

var allischecked = ($("#Form2 input:checkbox:not(checked)").length === 0);
svinto
+1 for also showing the JQuery version. Yeah, if this is all you want to do, JQuery is overkill. But if you have other validation, AJAX, etc., a one-line test is hard to beat.
GalacticCowboy
A: 

If you have a large amount of checkboxes that you don't want to loop through to test it might be more efficient to use this approach.

var checked = 0;

$("input[type=checkbox]").live("click", function() {
    if($(this).attr("checked")) checked++;
    else checked--;
}

Then you would be able to test like this.

if(checked === 0) {
    doSomething();
}