Checkbox frustration

Checkbox frustration

Hey all. Just thought I'd throw this out there and see if anyone has any insight.

I'm using a Facebox 'popup' div that allows users to choose specific fields for an Excel download. The fields all have checkboxes beside them to select/unselect. I'm trying to make it so they can save their current selections into a cookie so the div will reload with the same fields selected the next time they go to do a download.

I have code that reads the cookie when the popup is launched. It cycles through the fields, checking the appropriate checkboxes. It looks like this and it works like a charm:

function showOverlay(cookieName)
{
   // when showing the field chooser div
   // check for a user prefs cookie and
   // check the appropriate checkboxes
   var fields = "";
   
   fields = readCookie(cookieName);
   
   if (fields != '')
   {
      var fieldsArray = [];
   
      fieldsArray = fields.split(",");
      
      for (i=0; i<fieldsArray.length; i++)
      {
         // the 'value' is where I'm putting the db/field name
         // so we'll check the box with 'value = ' the field
         // found in the cookie
         $('input[value=' + fieldsArray[i] + ']').attr('checked',true);
      }
   }
   
}


When I go to save the user's currently checked boxes I run a similar function that looks to see what boxes are checked. I'm sure I could use jquery more effectively here, but for now each checkbox has an id of 'checkXX' so I can loop through numerically:


function getFields(num)
{
   fieldList = [];
   counter = 0;
      
   for (i=1; i < num + 1; i++)
   {
      if ( $('input[id=check' + i + ']').is(':checked') )
      {   
         //alert ( 'checked: ' + $('input[id=check' + i + ']').val());
         fieldList[counter] = $('input[id=check' + i + ']').val();
         counter++;
      }
      else
      {
         //alert ('not checked: ' + $('input[id=check' + i + ']').val());
         
      }
   }
   
   // comma list for storage in the cookie
   fieldListString = fieldList.join(",");
   
   return fieldListString;
}   



What's happening is that if I unclick any previously checked boxes (meaning any of the ones initally checked when the cookie was read), the saved result is ALWAYS the original value from the cookie. If I ADD checked boxes, the saved value is appended to, but unchecking the boxes seems to have no effect.

Any idea what I might be missing in this? It seems like my 'getFields' function isn't REALLY looking at the fields on the page. I'd appreciate any feedback if you have a clever idea. I'm no javascript expert, so I'm struggling at the best of times. Thanks!