Reputation: 1229
I have a 6 checkboxes checked/unchecked based on values from the database, the 5th checkbox is unchecked, to get the state, I use the snippet below:
var allCheckboxes = $(':input[name="cc_checkbox"]').map(function () {
return this.value;
}).get();
when I console.log(allCheckboxes)
I get ["1", "1", "1", "1", "0", "1"]
which is right.
But when I check/uncheck any of the checkboxes and console.log(allCheckboxes)
, I still get ["1", "1", "1", "1", "0", "1"]
Is this a normal a behaviour or is there some other syntax I need to use?
Upvotes: 2
Views: 381
Reputation: 337713
The issue is because the value
of a checkbox is always present. It doesn't change depending on whether or not the element is checked.
To solve this you could put the checked
property of the elements in to the array instead:
var allCheckboxes = $(':input[name="cc_checkbox"]').map(function () {
return this.checked;
}).get();
Given the example in your OP this would give you an array output of:
[true, true, true, true, false, true]
Upvotes: 2