Selecting checked values from check boxes

I'm getting values to my html table using java script. Then i want to passed checked check box values back to my server. But following script function always gives an empty selected array.

function find() {

    var dataObject = {};
    var selected = [];
    var i;

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

    for (var i = 0, len = selected.length; i < len; i++) {
        dataObject['id' + (i+1)] = (selected[i]);

    }

Here is the received html values

<td>
     <input type="checkbox" tr.id="0" value="0">
</td>

Answers 1

  • Try this.

    function findcheck() {
    
                var selected = [];
    
                $('input[type=checkbox]').each(function() {
                    var $this = $(this);
    
                    if ( $this.is(':checked') == true) {
                        selected.push( $this.val() );           
                    }
    
                });
    
            }
    
    findcheck();
    

    fiddlelink


Related Articles