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!