I have been wrecking my brains trying to figure out why I get an undefined error for the array statusIdValues when I hit the first push in the code below. Its initialised in a ready function and is pushed into when a checkbox state changes.
$(document).ready(function () {
var statusIdValues = [];
$(':checkbox').change(function () {
if ($(this).is(":checked")) {
statusIdValues.push($(this).attr("value"));
}
else {
var index= statusIdValues.indexOf($(this).attr("value"));
if (index > -1) {
statusIdValues.splice(index, 1);
}
});
});
Any help would be appreciated.
asked Mar 8, 2017 at 14:59
Bad Dub
1,5933 gold badges26 silver badges59 bronze badges
1 Answer 1
Try this, change in the else the variable "statusIdValues" for "index", and "array" for your actual array "statusIdValues", like this:
$(document).ready(function () {
var statusIdValues = [];
$(':checkbox').change(function () {
if ($(this).is(":checked")) {
statusIdValues.push($(this).attr("value"));
}
else {
var index = statusIdValues.indexOf($(this).attr("value"));
if (index > -1) {
statusIdValues.splice(index, 1);
}
});
});
answered Mar 8, 2017 at 15:03
JC Hernández
8074 silver badges14 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Bad Dub
Thats it, would you believe I also had a second set of eyes on this we both didnt spot that typo! Thank you.
fdomn-m
So, this is the answer? Then why did you update your question to match this? Is there even a question now?
Bad Dub
I updated it because I realised there was a typo, which coincidentally was also the problem which JC also saw and fixed.
lang-js
array.indexOf($(this).attr("value"));What is array ?