Checkbox logic not updating all boxes correctly

Issue #15 resolved
Paul Stasiuk repo owner created an issue

This block of code causes all of the checkboxes of other categories to check:

 //Re-populate the agencies scrollbox
    var rent_element = document.getElementById(agencies_scroll_box_id);
    for (var q=0;q<agency_list.length;q++){

      //Create the checkbox
      var checkbox = document.createElement("input");
      checkbox.type = "checkbox";
      checkbox.id = "agencyCheck"+q;
      checkbox.name = "agencyCheck";
      checkbox.setAttribute("onchange","boxChecked(this.form.agencyCheck,"+q+")");
      checkbox.value = (agency_list[q]);

      //Create the lable
      var label = document.createElement("checkLabel");
      label.setAttribute("name",agency_list[q]);
      label.setAttribute("type","checklabel");
      label.innerHTML = agency_list[q] +"<br />";

      //Check to see if the checkbox needs to be left checked
      if (agency_list[q].indexOf(checked_boxes) != -1 ){
        checkbox.checked = true;
      }

      //Append the lables to the scrollbox
      rent_element.appendChild(checkbox);
      rent_element.appendChild(label);
    //endfor    
    }

    /*
      I have no idea why this is here. Leaving it. @pstasiuk
    */
    parameters_list = parameters_list.filter(function(itm,i,parameters_list){
      return i==parameters_list.indexOf(itm);
    });

This code works fine:

   //Re-populate the agencies scrollbox
    var rent_element = document.getElementById(parameters_scroll_box_id);
    for (var q=0;q<parameters_list.length;q++){
      var checkbox = document.createElement("input");
      checkbox.type = "checkbox";
      checkbox.id = "parameterCheck"+q;
      checkbox.name = "parameterCheck";
      checkbox.setAttribute("onchange","boxChecked(this.form.parameterCheck,"+q+")");
      checkbox.value = (parameters_list[q]);

      //Create the lable
      var label = document.createElement("checkLabel");
      label.setAttribute("name",parameters_list[q]);
      label.setAttribute("type","checklabel");
      label.innerHTML = parameters_list[q] +"<br />";

      //Check to see if the checkbox should be left checked.
      if(typeof(checked_boxes)=="undefined"){
        if (parameters_list[q].indexOf(checked_boxes) != -1){
          checkbox.checked = true;
        }else{
          checked.checked = false;
        }
      }

      //Append the lables and the checkboxes to the root element.     
      rent_element.appendChild(checkbox);
      rent_element.appendChild(label);
    //endfor
    }

  //end$.getJSON
  });

Comments (5)

  1. Paul Stasiuk reporter

    Its because of this:

    If the array is of size 0, javascript apparently does not consider it initialized.

          //Check to see if the checkbox should be left checked.
          if(typeof(checked_boxes)=="undefined"){
            if (parameters_list[q].indexOf(checked_boxes) != -1){
              checkbox.checked = true;
            }else{
              checked.checked = false;
            }
          }
    
  2. Paul Stasiuk reporter

    Testing this block:

            if(checked_boxes.length!=0){
              if (user_list[q].indexOf(checked_boxes) != -1){
                checkbox.checked = true;
              }else{
                checked.checked = false;
              }
            }
    
  3. Paul Stasiuk reporter

    Done screwing with this. Wrote a helper function to determine if something is in an array or not.

            if(checked_boxes.length!=0){
              if (isInArray(user_list[q],checked_boxes)){
                checkbox.checked = true;
              }else{
                checkbox.checked = false;
              }
            }
    

    This is the helper function:

    function isInArray(string,array){
      for (var i = 0; i<array.length; i++){
        if(string = array[i]){
          return true;
        }
      }
      return false;
    }
    
  4. Paul Stasiuk reporter

    isInArray has an error in it, fixed it. Committing tested changes:

    function isInArray(string,array){
      for (var i = 0; i<array.length; i++){
        if(string == array[i]){
          return true;
        }
      }
      return false;
    }
    
  5. Log in to comment