Lamloumi Afif Lamloumi Afif - 1 year ago 49
HTML Question

Get checked values of checkbox list using jquery

I have a couple of checkbox with the name

. I'd like to get all the checked
's value and store it in a single string.

So tried this :

$('[name="etapes"]:checked').each(function() {
indexer = indexer + 1;
if (indexer == 1) selectedEtapes = $(this).val();
else selectedEtapes = selectedEtapes + "," + $(this).val();

but it didn't work. SO how can I fix this issue?

Answer Source

Easier solution is to use

var mapped = $('[name="etapes"]:checked').map(function() {
  return this.value;
<script src=""></script>
<input type="checkbox" name="etapes" value="Ouverte1" checked>
<input type="checkbox" name="etapes" value="Ouverte2">
<input type="checkbox" name="etapes" value="Ouverte3" checked>

Fix for your code:

You never accepted index argument,

var selectedEtapes = '';
$('[name="etapes"]:checked').each(function(index) {
  if (!index) selectedEtapes += $(this).val();
  else selectedEtapes += "," + $(this).val();