1

I have a bunch of checkboxes:

<div id="other-products">
  <input type="checkbox" name="First" value="1">
  <input type="checkbox" name="Second" value="2">
  <input type="checkbox" name="Third" value="3">
</div>
<span class="list-products"></span>

JS code like this, which generates all the names of the checked inputs.

$('#other-products').on('change', function() {
    var selectedProducts = [];
    $('#other-products input[type="checkbox"]:checked').each(function() {
      selectedProducts.push($(this).attr('name'));
      var showtimesAsString = selectedProducts.join(', ');
      $('.list-products').html(showtimesAsString);
    });
});

The problem is, when I select few checkboxes, then unselect all of them, last unselected checkbox title is shown in .list-products. Any ideas why it is not empty? Thanks!

2
  • 3
    When you uncheck the last checkbox, there is no checked item and so the each loop will not execute. Commented Feb 9, 2016 at 19:02
  • From api.jquery.com/change - "This event is limited to <input> elements, <textarea> boxes and <select> elements" Commented Feb 9, 2016 at 19:10

5 Answers 5

2

Try resetting the html content of the list-products outside of the loop.

https://jsfiddle.net/99x50s2s/188/

 $('#other-products').on('change', function() {
    var selectedProducts = [];
    var listProductsCtrl = $('.list-products');
    listProductsCtrl.html(''); //reset the values here
    $('#other-products input[type="checkbox"]:checked').each(function() {
      selectedProducts.push($(this).attr('name'));
      var showtimesAsString = selectedProducts.join(', ');
      listProductsCtrl.html(showtimesAsString);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

In addition to causing your script to not work as intended, setting showtimesAsString in $.each in inefficient. Move the 2 lines where you create the string and set the .html() outside of $.each().

$('#other-products').on('change', function() {
  var selectedProducts = [];

  $('#other-products input[type="checkbox"]:checked').each(function() {
    selectedProducts.push($(this).attr('name'));

  });

  var showtimesAsString = selectedProducts.join(', ');
  $('.list-products').html(showtimesAsString);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="other-products">
  <input type="checkbox" name="First" value="1">
  <input type="checkbox" name="Second" value="2">
  <input type="checkbox" name="Third" value="3">
</div>
<span class="list-products"></span>

Comments

0

You could change your code to something like the following:

$('#other-products').on('change', function() {
    var selectedProducts = $('#other-products input[type="checkbox"]:checked');
    var showtimesAsString = "";

    selectedProducts.each(function() {
       showtimesAsString += $(this).attr("name") + ",";
    });

    $('.list-products').html(showtimesAsString);
});

https://jsfiddle.net/5bu0sqjf/

Comments

0
$('#other-products').on('change', function() {
    var selectedProducts = [];
    $('.list-products').html('');
    $('#other-products input[type="checkbox"]:checked').each(function() {
      selectedProducts.push($(this).attr('name'));
      var showtimesAsString = selectedProducts.join(', ');
      $('.list-products').html(showtimesAsString);
    });
});

Comments

0

You can use jQuery's map() function to really simplify the code and avoid having to manually reset your array and output string.

$(function() {
  $('#other-products').on('change', function() {
    var names = $('#other-products input[type="checkbox"]:checked').map(function() {
      return $(this).attr('name');
    }).get();
    $('.list-products').html(names.join(', '));
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="other-products">
  <input type="checkbox" name="First" value="1">
  <input type="checkbox" name="Second" value="2">
  <input type="checkbox" name="Third" value="3">
</div>
<span class="list-products"></span>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.