javascript – Best way to get all selected checkboxes VALUES in jQuery-ThrowExceptions

Exception or error:

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

How to solve:

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();

Leave a Reply

Your email address will not be published. Required fields are marked *