tags:

views:

4635

answers:

4

Hello,

In a div, I have some checkbox. I'd like when I push a button get all the name of all check box checked. Could you tell me how to do this ?

<div id="MyDiv">
....
<td><%= Html.CheckBox("need_" + item.Id.ToString())%></td>
...
</div>

Thanks,

+10  A: 
$(document).ready(function() {
    $('#someButton').click(function() {
        var names = [];
        $('#MyDiv input:checked').each(function() {
            names.push(this.name);
        });
        // now names contains all of the names of checked checkboxes
        // do something with it
    });
});
TM
Why it's so easy when you give me the solution ?! ;-)
Kris-I
A: 

Not tested but should work:

$("#MyDiv td input:checked").each(function()
{
    alert($(this).attr("id"));
});
Time Machine
+1  A: 
var aArray = [];
window.$( "#myDiv" ).find( "input[type=checkbox][checked]" ).each( function()
{
  aArray.push( this.name );

});

You can put it in a function and execute on click of the button.

Brian