Bad Dub
Bad Dub

Reputation: 1593

jQuery Array Undefined Error

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.

Upvotes: 0

Views: 401

Answers (1)

JC Hernández
JC Hernández

Reputation: 807

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);
      }
    });
 });

Upvotes: 1

Related Questions