[jQuery] Autoselect specific checkboxes based on select dropdown
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.6000.16544" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial size=2><SPAN class=246235115-01112007>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.</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=246235115-01112007></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=246235115-01112007>The
dropdown:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=246235115-01112007></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=246235115-01112007><select
name="accesslevel" id="accesslevel"><BR> <option
value="">select an access level</option><BR>
<option value="6">25 / Low Dealer</option><BR>
<option value="5">50 / Medium-High
Dealer</option><BR> <option value="4">75 / High
Dealer</option><BR></select></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=246235115-01112007></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=246235115-01112007>One of 40 or 50
checkboxes:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=246235115-01112007><input
type="checkbox" name="permissionid" value="1119" /> // value is the only
thing that changes.</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=246235115-01112007></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=246235115-01112007>Here's my
code:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=246235115-01112007>// permission
arrays<BR>var arr25 = [1119,1123,1099]; // bottom permission level<BR>var arr50
= [1086,1083,1079]; // medium permission level<BR>var arr75 = [1081,1084,1082];
// top permission level</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=246235115-01112007>// assign change
handler<BR>$('#accesslevel').change(function(){<BR> // get the
TEXT of the selected option, not it's value<BR> var option =
$(':selected',this).text().split(' / ')[0];<BR> // make sure
the user didn't select the first option<BR> if (option !=
'select an access level') {<BR> // deselect
all
checkboxes<BR> $('input[name=permissionid]').removeAttr("checked");<BR> //
use the array for the selected permission
level<BR> var curArr =
eval('arr'+option);<BR> // loop over the
array<BR> for (o=0;o < curArr.length; o++)
{<BR> // check every
checkbox whose value is in this
array<BR> $('input[value='
+ curArr[o] +
']').attr('checked','checked');<BR> }<BR> }<BR>});<BR></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV align=left><SPAN style="FONT-SIZE: 14px"><B><FONT face="Century Gothic">
<DIV align=left><SPAN class=159130414-05012007><FONT face=Arial size=2><SPAN
class=159130414-05012007><FONT face=Arial
size=2>____________________________________</FONT></SPAN></FONT></SPAN></DIV>
<DIV><SPAN class=159130414-05012007><FONT face=Arial size=2><SPAN
class=159130414-05012007></SPAN></FONT></SPAN> </DIV>Andy
Matthews<BR></FONT></B></SPAN><SPAN style="FONT-SIZE: 11px"><FONT
face="Century Gothic"><SPAN
style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'Century Gothic'; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA; mso-no-proof: yes">Senior
ColdFusion Developer</SPAN><BR></FONT></SPAN><SPAN style="FONT-SIZE: 11px"><FONT
face="Century Gothic">Office: 877.707.5467 x747<BR>Direct:
615.627.9747<BR>Fax: 615.467.6249</FONT></SPAN></DIV>
<DIV><SPAN style="FONT-SIZE: 11px"><FONT
face="Century Gothic">amatthews@dealerskins.com<BR><A
href="http://www.dealerskins.com/">www.dealerskins.com</A></FONT></SPAN></DIV>
<DIV> </DIV></BODY></HTML>