0

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.

2
  • 2
    array.indexOf($(this).attr("value")); What is array ? Commented Mar 8, 2017 at 15:01
  • typo, ill update Commented Mar 8, 2017 at 15:12

1 Answer 1

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);
      }
    });
 });
Sign up to request clarification or add additional context in comments.

3 Comments

Thats it, would you believe I also had a second set of eyes on this we both didnt spot that typo! Thank you.
So, this is the answer? Then why did you update your question to match this? Is there even a question now?
I updated it because I realised there was a typo, which coincidentally was also the problem which JC also saw and fixed.

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.