views:

3132

answers:

3

So I have these checkboxes:

<input type="checkbox" name="type" value="4" />
<input type="checkbox" name="type" value="3" />
<input type="checkbox" name="type" value="1" />
<input type="checkbox" name="type" value="5" />

And so on. There are about 6 of them and are hand-coded (i.e not fetched from a db) so they are likely to remain the same for a while.

My question is how I can get them all in an array (in javascript), so I can use them while making an AJAX $.post request using Jquery.

Any thoughts?

Edit: I would only want the selected checkboxes to be added to the array

+10  A: 

Formatted :

$("input:checkbox[name=type]:checked").each(function()
{
    // add $(this).val() to your array
});

Hopefully, it will work.

ybo
Sorry, I don't know why the formatting fails :(
ybo
Ok, formatted correctly now :)
ybo
select all and click on the code in the text editor
Barbaros Alp
+1  A: 

This should do the trick:

$('input:checked');

I don't think you've got other elements that can be checked, but if you do, you'd have to make it more specific:

$('input:checkbox:checked');

$('input:checkbox').filter(':checked');
Georg
A: 

I didnt test it but it should work

<script type="text/javascript">
var selected = new Array();

$(document).ready(function() {

  $("input:checkbox[name=type]:checked").each(function() {
       selected.push($(this).val());
  });

});

</script>
Barbaros Alp