views:

7843

answers:

6

I'm using Zend_Form to output a set group of checkboxes:

<label style="white-space: nowrap;"><input type="checkbox" name="user_group[]" id="user_group-20" value="20">This Group</label>

With a normal HTTP Post these values are passed as an array, but when I'm somewhat stumped on how to grab all the values using jQuery. I figured I can select the group using:

$("input[@name='user_group[]']").val()

but that just grabs the value of the first checkbox in the list regardless of if it is checked of not. Any ideas?

A: 

You can have a javascript variable which stores the number of checkboxes that are emitted, i.e in the <head> of the page:

<script type="text/javascript">
var num_cboxes=<?php echo $number_of_checkboxes;?>;
</script>

So if there are 10 checkboxes, starting from user_group-1 to user_group-10, in the javascript code you would get their value in this way:

var values=new Array();
for (x=1; x<=num_cboxes; x++)
{
   values[x]=$("#user_group-" + x).val();
}
Click Upvote
Why the downvote?
Click Upvote
+10  A: 

You could use the checked selector to grab only the selected ones (negating the need to know the count or to iterate over them all yourself):

$("input[name='user_group[]']:checked")

With those checked items, you can either create a collection of those values or do something to the collection:

var values = new Array();
$.each($("input[name='user_group[]']:checked"), function() {
  values.push($(this).val());
  // or you can do something to the actual checked checkboxes by working directly with  'this'
  // something like $(this).hide() (only something useful, probably) :P
});
patridge
`@` is not required in `$.each($("input[@name='user_group[]']:checked"), function() {`
Ismail
@Ismail, it has been updated for post-1.3 jQuery. Version 1.3 was released after the answer was posted (posted Jan 06, 2009, 1.3 released Jan 14, 2009). Attribute selectors shouldn't be used by anyone using v1.3 or newer.
patridge
Can't `[name='user_group[]']` be called as attribute selector. I still use it in 1.4.2. Is there are better alternative now?
Ismail
That was a typo on my part. Attribute selectors [with "@"] shouldn't be used by anyone using v1.3 or newer.
patridge
A: 

I'm not 100% entirely sure how you want to "grab" the values. But if you want to iterate over the checkboxes you can use .each like so:

("input[@name='user_group[]']").each( function() { alert($(this).val()); });

Of course a better selector is available:

$(':checkbox')

Rockitsauce
A: 

I just shortened the answer I selected a bit:

var selectedGroups  = new Array();
$("input[@name='user_group[]']:checked").each(function() {
    selectedGroups.push($(this).val());
});

and it works like a charm, thanks!

dragonmantank
A: 

mhata dzenyu mese. its actually

var selectedGroups = new Array(); $(".user_group[checked]").each(function() { selectedGroups.push($(this).val()); });

+4  A: 

I'm not sure about the "@" used in the selector. At least with the latest jQuery, I had to remove the @ to get this to function with two different checkbox arrays, otherwise all checked items were selected for each array:

var items = [];
$("input[name='items[]']:checked").each(function(){items.push($(this).val());});

var about = [];
$("input[name='about[]']:checked").each(function(){about.push($(this).val());});

Now both, items and about work.

Pat Osterday
I believe the @ was removed in jQuery 1.3 +1
Thorpe Obazee