0

I am working on this demo.
Why am I not able to get the checkbox values on checking and unchecking the checkboxes?

// find elements
$('input:checkbox[name=product]').on('change', function() {
    var val = '';
    $('input:checkbox[name=product]:checked').each(function() {
        val += $(this).val();
    });
    switch (val) {
        case 'all':
             console.log(val);
            break;
        case 'women':
             console.log(val);
            break;
        case 'men':
             console.log(val);
            break;
   case 'men':
             console.log(val);
            break;
       }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="product" value="all" checked />All Genders <br />
<input type="checkbox" name="product" value="women" />Women <br />
<input type="checkbox" name="product" value="men" />Men<br />
<input type="checkbox" name="product" value="bi" />Bi<br />
Adriano
  • 3,788
  • 5
  • 32
  • 53
Mona Coder
  • 6,212
  • 18
  • 66
  • 128
  • What logic are you trying to create? I'm sure it can be done in a much less verbose manner. – Rory McCrossan May 28 '18 at 17:17
  • trying to update a list of selected checkboxed in an array by updating the states of checkbox – Mona Coder May 28 '18 at 17:18
  • In which case just build the entire array whenever one box is changed, like this: https://stackoverflow.com/questions/19766044/best-way-to-get-all-selected-checkboxes-values-in-jquery/19766067#19766067. Also note that you aren't using arrays at all in the code snippet you've shown. – Rory McCrossan May 28 '18 at 17:19

2 Answers2

1

switch condition should come inside each loop.

// find elements
$('input:checkbox[name=product]').on('change', function() {
    var valueArray = [];
    $('input:checkbox[name=product]:checked').each(function() {
        var val = $(this).val();
        valueArray.push(val);

        switch (val) {
            case 'all':
                console.log(val);
                break;
            case 'women':
                console.log(val);
                break;
            case 'men':
                console.log(val);
                break;
            case 'men':
                console.log(val);
                break;
        }
    });
    console.log("final checked box array");
    console.log(valueArray);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="product" value="all" checked />All Genders <br />
<input type="checkbox" name="product" value="women" />Women <br />
<input type="checkbox" name="product" value="men" />Men<br />
<input type="checkbox" name="product" value="bi" />Bi<br />
Nishant Dixit
  • 5,388
  • 5
  • 17
  • 29
0

You just need to console log the checked values, no need of any switch case there.

// find elements
$('input:checkbox[name=product]').on('change', function() {
    var val = '';

    $('input:checkbox[name=product]:checked').each(function() {
        val = $(this).val();
        console.log(val)
    });
    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="product" value="all" checked />All Genders <br />
<input type="checkbox" name="product" value="women" />Women <br />
<input type="checkbox" name="product" value="men" />Men<br />
<input type="checkbox" name="product" value="bi" />Bi<br />
Nandita Sharma
  • 13,287
  • 2
  • 22
  • 35