Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to get all selected checkboxes VALUES in jQuery [duplicate]

I am looking to get all checkboxes' VALUE which have been selected through jQuery.

like image 873
hercules Avatar asked Nov 04 '13 10:11

hercules


1 Answers

You want the :checkbox:checked selector and map to create an array of the values:

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

If your checkboxes have a shared class it would be faster to use that instead, eg. $('.mycheckboxes:checked'), or for a common name $('input[name="Foo"]:checked')

- Update -

If you don't need IE support then you can now make the map() call more succinct by using an arrow function:

var checkedValues = $('input:checkbox:checked').map((i, el) => el.value).get();
like image 150
Rory McCrossan Avatar answered Nov 19 '22 19:11

Rory McCrossan