There main issue with your approach is in this line:
CountSelectedCB[0] = $(this).attr("id");
The way you are using the array CountSelectedCB
, you are always inserting the id of a check box at the first position (index 0) so you will always end up with one id no matter how many checkboxes are checked (or no ids) if no checkboxes are checked.
You should change the line above to:
CountSelectedCB.push($(this).attr("id"));
Also, since CountSelectedCB
is in a higher scope then displayCheckbox
function, it keeps it's values between calls to displayCheckbox
so on each firing of the change
event, you have to clear the array before adding the updated list of checked ids. Otherwise, your updated list of ids will get appended to previously stored list.
Here is how your code could look like (sample Fiddle):
function displayCheckbox(){
$("input:checkbox").change(function() {
selectedCB = [];
notSelectedCB = [];
CountSelectedCB.length = 0; // clear selected cb count before adding the updated ones
$("input:checkbox").each(function() {
if ($(this).is(":checked")) {
CountSelectedCB.push($(this).attr("id")); // add id to count selected cb
}
});
$('input[name=selectedCB]').val(CountSelectedCB);
});
}
$(document).ready(displayCheckbox);
CountSelectedCB = [];
function displayCheckbox(){
$("input:checkbox").change(function() {
selectedCB = [];
notSelectedCB = [];
CountSelectedCB.length = 0; // clear selected cb count
$("input:checkbox").each(function() {
if ($(this).is(":checked")) {
CountSelectedCB.push($(this).attr("id"));
}
});
$('input[name=selectedCB]').val(CountSelectedCB);
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="cb1">Checkbox #1
<input type="checkbox" id="cb2">Checkbox #2
<input type="checkbox" id="cb3">Checkbox #3
<hr />
<input name="selectedCB">
Here is another way of achieving this (sample Fiddle)
Essentially, you initially add a change
listener to every check box (on document ready or some other event depending on your application logic) that will print all checked boxes.
var checkboxes = $("input:checkbox"); // save checkboxes into a variable so we don't waste time by looking them up each time
// add change listeners to checkboxes
$.each(checkboxes, function() {
$(this).change(printChecked);
})
And you make a function to actually print ids of the checked boxes:
function printChecked() {
var checkedIds = [];
// for each checked checkbox, add it's id to the array of checked ids
checkboxes.each(function() {
if($(this).is(':checked')) {
checkedIds.push($(this).attr('id'));
}
});
$('input[name=selectedCB]').val(checkedIds);
}
$(document).ready(displayCheckbox);
function displayCheckbox() {
var checkboxes = $("input[type=checkbox]");
var displayField = $('input[name=selectedCB]');
$.each(checkboxes, function() {
$(this).change(printChecked);
})
function printChecked() {
var checkedIds = [];
// for each checked checkbox, add it's id to the array of checked ids
checkboxes.each(function() {
if($(this).is(':checked')) {
checkedIds.push($(this).attr('id'));
}
});
displayField.val(checkedIds);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="cb1">Checkbox #1
<input type="checkbox" id="cb2">Checkbox #2
<input type="checkbox" id="cb3">Checkbox #3
<hr />
<input name="selectedCB">