views:

98

answers:

2

I have a form with a bunch of checkboxes, and a hidden field that contains CSV data. Basically, I need the checkboxes to be ticked based on the value of the hidden field:

<form>

    <input type="checkbox" id="01" name="01" />
    <input type="checkbox" id="02" name="02" />
    <input type="checkbox" id="03" name="03" />

    <input type="hidden" value="03,01," />

</form>

So, in the example above checkboxes 01 and 03 need to be ticked when the page has finished loading. I also need to ensure that when the value of the checkboxes changes, so too does the hidden CSV field (though this could perhaps be done when the form submits?).

My limited jQuery knowledge has got me nowhere with this, any help would be much appreciated. (I know that some of this would be best done server-side, but I have no access to anything but javascript).

The answer below does most of what I want. I think that using the variables created, it should be possible to re-populate the CSV field when the form submits. ie, using selected_ids to add the ids of the checked boxes onto the end of the CSV (if they aren't already present). Can this be done?

+1  A: 
var csvValue = $('#csv-input').val();
var selected_ids = csvValue.split(',');
for (var i = 0; i < selected_ids.length; i++)
{
    var selected_id = selected_ids[i];
    $('#' + selected_id).attr('checked', 'checked');
}
Sjoerd
Okay, thanks. Talk to me like the idiot that I am. I can see that 'selected_ids' returns an array of all the CSV data in #csv-input, but the checkboxes with the corresponding ids are not being checked. I tried changing the fifth line to $('#' + '0' + i).attr('checked', 'checked');But that just checks the first two checkboxes (the total length of the array?)Is it possible to re-work the above so that the ids of all the checkboxes can be anything arbitrary (ie, more descriptive) whilst still keeping the brevity of that code?
Ed-M
I've edited the code, it didn't work correctly. Now, it retrieves the id from selected_ids. You can use any id name, not just numbers.
Sjoerd
Thank you so much, that is perfect.
Ed-M
A: 
$(':checkbox').bind('change', function(event){
    if (this.checked == true){
      // add to hidden field
      var tempIdStr = $('#cvs-input').val();
      // if not in hidden value already
      if (tempIdStr.indexOf(this.id) > -1){
          tempIdStr += "id,"; // or ", id" depending on how you are seperating the ids
         $('#cvs-input').val(tempIdStr);
      }
    } else {
    // remove from hidden field
      var tempIds = $('#cvs-input').val().split(',');
      var index = tempIds.indexOf(this.id);
      if (index > -1){
          tempIds.splice(index, 1);
          $('#cvs-input').val(tempIds.join(',');
    }
});
Nimnam1
haven't tested this but I think this should work
Nimnam1
This line:var tempIdStr += "id," // or ", id" depending on how you are seperating the idsthrows up an error. What exactly is this part doing, and how should I amend it?
Ed-M
the value of cvs-input is a comma separated list of ids. depending on if the value already has a comma at the end of the list you wouldn't want to add another because it would look like exp. 01, , 02 however if you didn't add commas at the end you need to include a comma at the beginning of the id you're adding.
Nimnam1
sorry forgot the end of the if statement... ya probably was confusing
Nimnam1
tempIdStr is being declared twice isn't it? This still doesn't work, but I can't figure out exactly what is going wrong.
Ed-M
ya tempIdStr was being declared twice in the checked== true portion I also was for some reason checking at indexOf() > 0 when it should have been > -1try this
Nimnam1
I've tried to get this to work by making changes, but it keeps throwing up errors. Have you managed to get this working?
Ed-M
Can anyone help with getting this to work? The code above seems close...
Ed-M