views:

1909

answers:

3

Hello all, I've run into a bit of an issue. Here's a brief explanation.

I have 12 check boxes on a standard form. What I need to do is loop through each of them and learn which ones are checked and which ones are unchecked.

Using this, I can then build a string which I then enter into a database field. Here is an example.

(Check1 - checked) (Check2 - not checked) (Check3 - checked)

1,0,1

So far, I've got this bit of code.

$('input[type=checkbox]').each(function () {
           if (this.checked) {
               console.log($(this).val());
}

It works perfectly except that it only brings back the checked ones, not all.

Sorry for the newb question. I'm kinda new to jquery.

Thanks in advance.

+2  A: 

Using Selectors

You can get all checked checkboxes like this:

var boxes = $(":checkbox:checked");

And all non-checked like this:

var nboxes = $(":checkbox:not(:checked)");

You could merely cycle through either one of these collections, and store those names. If anything is absent, you know it either was or wasn't checked. In PHP, if you had an array of names which were checked, you could simply do an in_array() request to know whether or not any particular box should be checked at a later date.

Serialize

jQuery also has a serialize method that will maintain the state of your form controls. For instance, the example provided on jQuery's website follows:

single=Single2&multiple=Multiple&multiple=Multiple3&check=check2&radio=radio2

This will enable you to keep the information for which elements were checked as well.

Jonathan Sampson
+1  A: 

You can loop through all of the checkboxes by writing $(':checkbox').each(...).

If I understand your question correctly, you're looking for the following code:

var str = "";

$(':checkbox').each(function() {
    str += this.checked ? "1," : "0,";
});

str = str.substr(0, str.length - 1);    //Remove the trailing comma

This code will loop through all of the checkboxes and add either 1, or 0, to a string.

SLaks
Thanks. Will give this a try as well.
Richard M
It worked. Thanks. I really appreciate it.
Richard M
You could also use the value attribute. If you are trying to create a binary value, then give each check box an incramenting power of 2 and just add the checked ones up, the sum in binary will be the same as appending ones and zeros. You will be limited to 32 check boxes (largest numerical value is 2^32 in JavaScript [I think, could be 64]. A better database design would be more flexible)
Jason Sperske
A: 

To build a result string exactly in the format you show, you can use this:

var sList = "";
$('input[type=checkbox]').each(function () {
    sList += "(" + $(this).val + "-" + (this.checked ? "checked" : "not checked") + ")";
}
console.log (sList);

However, I would agree with @SLaks, I think you should re-consider the structure into which you will store this in your database.

EDIT: Sorry, I mis-read the output format you were looking for. Here is an update:

var sList = "";
$('input[type=checkbox]').each(function () {
    var sThisVal = (this.checked ? "1" : "0");
    sList += (sList=="" ? sThisVal : "," + sThisVal);
}
console.log (sList);
Ed Schembor
As I understand, he wants a string on `1`s and `0`s.
SLaks
Thanks. I'll try this out and let you know.
Richard M
Hmm. I tried this, but it displayed the entire js file in the console log.
Richard M