It is necessary to add checked checkboxes to the program array.

var program = []; for(var i=0; i<10; i++) { $('#pre_content5').append('<input type="checkbox" id="checkbox" name = "sam" value = "'+i+'"/>'); } 

I know that using jQuery you can select all the checked

 $('input:checkbox:checked') 

How exactly values ​​of checkboxes fill in an array? Nr when pressing the button.

    2 answers 2

    there is such a thing .map ():

     var arr=$('input:checkbox:checked').map(function() {return this.value;}).get(); 

    demo

    dock

      UPDATE. As @eicto noted, it's really better to use map in conjunction with the change event. It should be something like that.

       $('input:checkbox').on('change', function (ev) { program = $('input:checkbox:checked').map(function() { return $(this).val(); }).get(); }); 

      If the "value" means the value field, then something is possible.

       var program = []; function scanChecboxes() { program = []; $('input:checkbox:checked').each(function () { program.push($(this).val()); }); } 

      And call this function when the state of one of the checkboxes changes. The safest mousedown event.

       $('input:checkbox').each(function () { $(this).mousedown(scanCheckboxes); }); 
      • The second part of your answer is in doubt. firstly, it is advisable to use on here, secondly, mousedown is a bad event for checkboxes - zb ' ' '
      • @eicto I agree. Updated your answer. - khaos
      • @khaos, I'm talking about events, I do not see an update. - zb '