GEOFFREY MWANGI GEOFFREY MWANGI - 3 months ago 9
HTML Question

Iterating through all selected checkboxes and get the value of input boxes in jquery

I have this code whereby in a table row there are checkboxes and input boxes, I would like to get the values of all input boxes whose checkboxes are checked

This is the html

<table>
<tr>
<td><input type="checkbox" name="new" value="37"></td>
<td><input type="text" name="new" id="quantity" class="37"></td>
</tr>

<tr>
<td><input type="checkbox" name="new" value="38"></td>
<td><input type="text" name="new" id="quantity" class="38"></td>
</tr>
.....#this continue
</table>


Note that the class of the input boxes is the same as the value of checkboxes

Currently am using this code to check all the checked checkboxes

var marked = new Array();
var k = 0;
$('input:checked').each(function(index,value) {

marked[k] = value;
k++;
alert(k);
});
alert($('input[name="new"]:checked').serialize());


How can i change it to return the value of the input boxes

Answer

Try this:

$(document).ready(function(){
        $( "input[type=checkbox]" ).each(function(){
            if($(this).is(':checked'))
            {
                var value = $(this).closest('tr').find($( "input[type=text]" )).val();
                alert(value);
            }
        });
    });
Comments