A coworker needed to be able to select a series of checkboxes based on the
value of a select dropdown. Here's what I came up with...I'm wondering how
it could be improved.
 
The dropdown:
 
<select name="accesslevel" id="accesslevel">
    <option value="">select an access level</option>
    <option value="6">25 / Low Dealer</option>
    <option value="5">50 / Medium-High Dealer</option>
    <option value="4">75 / High Dealer</option>
</select>
 
One of 40 or 50 checkboxes:
<input type="checkbox" name="permissionid" value="1119" /> // value is the
only thing that changes.
 
Here's my code:
// permission arrays
var arr25 = [1119,1123,1099]; // bottom permission level
var arr50 = [1086,1083,1079]; // medium permission level
var arr75 = [1081,1084,1082]; // top permission level
 
// assign change handler
$('#accesslevel').change(function(){
   // get the TEXT of the selected option, not it's value
   var option = $(':selected',this).text().split(' / ')[0];
   // make sure the user didn't select the first option
   if (option != 'select an access level') {
      // deselect all checkboxes
      $('input[name=permissionid]').removeAttr("checked");
      // use the array for the selected permission level
      var curArr = eval('arr'+option);
      // loop over the array
      for (o=0;o < curArr.length; o++) {
         // check every checkbox whose value is in this array
         $('input[value=' + curArr[o] + ']').attr('checked','checked');
      }
   }
});

 
____________________________________
 
Andy Matthews
Senior ColdFusion Developer
Office:  877.707.5467 x747
Direct:  615.627.9747
Fax:  615.467.6249
[EMAIL PROTECTED]
www.dealerskins.com <http://www.dealerskins.com/> 
 

Reply via email to