77

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hercules
  • 783
  • 1
  • 7
  • 6

1 Answers1

265

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();
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 2
    works like charm, i would prefer it as comma seperated string... – hercules Nov 04 '13 at 11:19
  • 22
    .join(',') does it – hercules Nov 04 '13 at 11:22
  • 1
    I got comma separated values without using .join('). I am using jQuery 1.10.2. And thanks for saving me time and extra lines of unnecessary lines of code, for me it works just perfect. – zeeshan Jan 09 '14 at 22:43
  • 1
    sorry to say that but it will always annoys me: `this.value`. Once you've started to use jQuery, so let's `return $(this).val();` – vaso123 Mar 01 '17 at 14:45
  • 2
    @vaso123 It's a personal preference as the performance difference is so slight, but I don't see the point creating a jQuery object for a property already available from `this`. – Rory McCrossan Mar 01 '17 at 14:46
  • Not consecvent. A 3rd users come, and starting to implement his own code as he thinkig, let's say add `$` to a jQuery object vairiable, and when a new developer need to maintain the code, he will found `$(this)`, `$this`, `this`, so he need to take time while figure out the puzzle, and refactor this not clean code. But it's your preference... – vaso123 Mar 01 '17 at 15:06
  • 4
    Well, code should be made to the best standards, not the lowest simply so inexperienced developers can understand it. – Rory McCrossan May 11 '17 at 07:13