John Black
John Black

Reputation: 53

Adding the values of an unknown number of checked checkboxes to an array

How do i create an array from an unknown number of checkboxes, adding the values to the array using jquery? and then posting the array?

Upvotes: 1

Views: 92

Answers (2)

Jashwant
Jashwant

Reputation: 28995

var arr = $.map($('input[type="checkbox"]:checked'),function(checkbox){
                    return checkbox.value;
                })

Upvotes: 1

Giovanni Della Cagna
Giovanni Della Cagna

Reputation: 148

You can try this:

function updateTextArea() {         
     var allVals = [];
     $('#c_b :checked').each(function() {
       allVals.push($(this).val());
     });
     $('#t').val(allVals)
  }
 $(function() {
   $('#c_b input').click(updateTextArea);
   updateTextArea();
 });

Upvotes: 1

Related Questions