35

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,

TM.
  • 108,298
  • 33
  • 122
  • 127
TheBoubou
  • 19,487
  • 54
  • 148
  • 236

8 Answers8

83
$(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.
  • 108,298
  • 33
  • 122
  • 127
  • Why it's so easy when you give me the solution ?! ;-) – TheBoubou Aug 17 '09 at 12:20
  • I was unable to get this array in the java, I used string[] but still. The error is when passing the variable via .post method. com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn Error setting expression 'names[]' with value '[Ljava.lang.String;@4a004a0' – changeme Jun 14 '12 at 21:36
  • 1
    [You can also use `.map()`](http://stackoverflow.com/questions/1287592/get-checkbox-list-values-with-jquery/27956423#27956423). – Josh Crozier Jan 15 '15 at 03:41
10

Since nobody has mentioned this..

If all you want is an array of values, an easier alternative would be to use the .map() method. Just remember to call .get() to convert the jQuery object to an array:

Example Here

var names = $('.parent input:checked').map(function () {
    return this.name;
}).get();

console.log(names);

var names = $('.parent input:checked').map(function () {
    return this.name;
}).get();

console.log(names);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
    <input type="checkbox" name="name1" />
    <input type="checkbox" name="name2" />
    <input type="checkbox" name="name3" checked="checked" />
    <input type="checkbox" name="name4" checked="checked" />
    <input type="checkbox" name="name5" />
</div>

Pure JavaScript:

Example Here

var elements = document.querySelectorAll('.parent input:checked');
var names = Array.prototype.map.call(elements, function(el, i) {
    return el.name;
});

console.log(names);

var elements = document.querySelectorAll('.parent input:checked');
var names = Array.prototype.map.call(elements, function(el, i){
    return el.name;
});

console.log(names);
<div class="parent">
    <input type="checkbox" name="name1" />
    <input type="checkbox" name="name2" />
    <input type="checkbox" name="name3" checked="checked" />
    <input type="checkbox" name="name4" checked="checked" />
    <input type="checkbox" name="name5" />
</div>
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
4
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
  • 3,571
  • 7
  • 44
  • 70
3

Not tested but should work:

$("#MyDiv td input:checked").each(function()
{
    alert($(this).attr("id"));
});
2

You should use map for this.

$('input[type=checkbox]:checked').map(function(i, e) { 
    return $(e).val(); 
});
Sam
  • 116
  • 2
  • 4
1

Try this one..

var listCheck = [];
console.log($("input[name='YourCheckBokName[]']"));
$("input[name='YourCheckBokName[]']:checked").each(function() {
     console.log($(this).val());
     listCheck .push($(this).val());
});
console.log(listCheck);
sonida
  • 4,411
  • 1
  • 38
  • 40
0
var nameCheckBoxList = "myCheckListName";
var selectedValues = $("[name=" + nameCheckBoxList + "]:checked").map(function(){return this.value;});
0

You can try this...

$(document).ready(function() {
        $("button").click(function(){
            var checkBoxValues = [];
            $.each($("input[name='check_name']:checked"), function(){
                checkBoxValues.push($(this).val());
            });

            console.log(checkBoxValues);

        });
    });
aphoe
  • 2,586
  • 1
  • 27
  • 31