views:

1207

answers:

8

I have a form that contains a bunch of checkboxes. Before submitting the form, I'd like to grab the values of the checkboxes and stick them into a text field, then submit that.

So I might have: Red Orange X Yellow Blue X Green

And I'd like my textfield to end up with the content "Yellow, Green" .

This doesn't seem too complicated, but I am totally out of my league. I already use jQuery for a few other things so I have the framework ready, if that makes it easer.

Thanks for any advice --

A: 

(Doh, that was supposed to be a list. 5 colors, with only two of them checked. You'd think I'd look at the preview box, but that's too easy... :)

You know you can also edit your question if you have made a mistake :) Just for future reference since you are new to the site.
Andrew Van Slaars
A: 

First hack at it (without testing):

var serializedCheckboxes = '';
$("input type='checkbox'").each(function() {
 if($(this).attr("checked")) {
  serializedCheckboxes += $(this).attr("value") + ',';
 }
});

$("input name='allchecks').attr("value", serializedCheckboxes);
Josh
A: 

fix to Josh code:

var serializedCheckboxes = '';
$("input[type=checkbox]").each(function() {
 if($(this).attr("checked")) {
  serializedCheckboxes += (serializedCheckboxes != '' ? ', ' : '') + $(this).attr("value");
 }
});

$("input[name=allchecks]").attr("value", serializedCheckboxes);

After my fix you should not get extra "," after last option.

Falco Foxburr
Nice catch and good improvement.
Josh
A: 

Wow, cool. What do I do to make that fire when the form is submitted?

+1  A: 

Just use this code:

$(function(){
    $('#YourFormID').bind('submit',function(){
     var serializedCheckboxes = '';
     $("input[type=checkbox]").each(function() {
      if($(this).attr("checked")) {
      serializedCheckboxes += (serializedCheckboxes != '' ? ', ' : '') + $(this).attr("value");
      }
     });
     $("input[name=allchecks]").attr("value", serializedCheckboxes);
    });
});

It starts when page is loaded and bind to "submit" event of your form correct function

Falco Foxburr
Forgot, would be wise to use ,join(stuff,',') instead of manually building it into a local string variable.
Josh
A: 

Ah, OK. I knew it had to be something simple like that! One last tiny question... I only want this code to fire if the form in question has checkboxes. So if the form is checkbox-free, I don't want the code to fire at all. Is there an equivalent to a php "ifexists"?

A: 

So, more improvements. Add a class name (in my example "colorCheckBoxes") to all checkboxes with colors in your form (if checkboxes exists) - this help avoid problems when you want to use different additional checkboxes in your form for other purposes than selecting colors. Then use this code:

$(function(){
    $('#YourFormID').bind('submit',function(){
     var serializedCheckboxes = '';
     $(this).find("input[type=checkbox]").filter('.colorCheckBoxes').each(function() {
      if($(this).attr("checked")) {
      serializedCheckboxes += (serializedCheckboxes != '' ? ', ' : '') + $(this).attr("value");
      }
     });
     if (serializedCheckboxes != '')
      $(this).find("input[name=allchecks]").attr("value", serializedCheckboxes);
    });
});
Falco Foxburr
A: 

Oh, that's great! Thank you so much -- you have totally saved my bacon. :)