Link to home
Start Free TrialLog in
Avatar of lostboyinsofla
lostboyinsoflaFlag for United States of America

asked on

Checkboxs act like radio buttons

I have this function:

<script language="javascript">
function ManageChkBoxes(form, selname) {
for (var ax=0; ax < form.elements.length; ax++) {
    var elem = form.elements[ax];
    if (elem.type == 'checkbox') {
        if (elem.name.match(selname) == null) { elem.checked = false;}
    }
}
return;
}
</script>

With this form(simplified for this question):
<form action="index.cfm?zmethod=apply" method="post" name="form_apply" id="form_apply">
     <table>
          <cfset cnt = 0>
          <cfoutput query="queryList">
          <tr>
                <td><input name="ignore_#cnt#" type="checkbox" value="" onclick="ManageChkBoxes(this.form, this.name);" /></td>
                <td><input name="apply_#cnt#" type="checkbox" value="#additionals#" onclick="ManageChkBoxes(this.form, this.name);" /></td>
         </tr>
         <cfset cnt = incrementValue(cnt)>
         </cfoutput>
     </table>
</form>

There is an ignore and an apply checkbox for each row.  I don't want the user to be able to check both boxes.  If the apply box is checked and the user checks ignore then the apply checkbox should uncheck and vice versa.   Each row is a different group so if the user check apply on another row it would have no effect on any other row then it's own.  I hope this makes sense.

What's happening if that if a user checks of apply in row 1 and the checks off apply in row 2, row 1's apply checkbox will uncheck.  This isn't suppose to happen.  Also if row 1,2 and 3's apply checkboxs are checked and the user goes to check row 5's ignore box then all the apply checkbox's uncheck.

Hope I was clear enough.

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lostboyinsofla

ASKER

Too cool.  Thanks for the answer.
My recommandation is simmelar:

<form action="index.cfm?zmethod=apply" method="post" name="form_apply" id="form_apply">
     <table>
          <cfset cnt = 0>
          <cfoutput query="queryList">
          <tr>
                <td><input name="ignore_#cnt#" type="checkbox" value="" onclick="ManageChkBoxes(this);" /></td>
                <td><input name="apply_#cnt#" type="checkbox" value="#additionals#" onclick="ManageChkBoxes(this);" /></td>
         </tr>
         <cfset cnt = incrementValue(cnt)>
         </cfoutput>
     </table>
</form>


<script language="javascript">
function ManageChkBoxes(theBox) {
  if(theBox.checked){
     var theForm = theBox.form;
     var nPart = theBox.name.split("_");
     if(nPart[0]=="apply"){
       theForm["ignore_"+nPart[0]].checked=false;
     } else {
       theForm["apply_"+nPart[0]].checked=false;
     }
  }
}
</script>

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Author: Third Santor</title>
<script>
  function ManageChkBoxes(obj){
    var index = obj.name.split('_')[1];
      var chkname1 = 'apply_';
      var chkname2 = 'ignore_';

    var chk1 = eval('obj.form. ' + (chkname1 + index));
      var chk2 = eval('obj.form. ' + (chkname2 + index));

      chk1.checked = obj.name.indexOf(chkname1)==0?obj.checked:!obj.checked;
      chk2.checked = obj.name.indexOf(chkname2)==0?obj.checked:!obj.checked;
}
</script>
</head>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
<form action="index.cfm?zmethod=apply" method="post" name="form_apply" id="form_apply">
     <table>
          <tr>
                <td><input name="ignore_0" type="checkbox" value="" onclick="ManageChkBoxes(this);" /></td>
                <td><input name="apply_0" type="checkbox" value="#additionals#" onclick="ManageChkBoxes(this);" /></td>
         </tr>
             <tr>
                <td><input name="ignore_1" type="checkbox" value="" onclick="ManageChkBoxes(this);" /></td>
                <td><input name="apply_1" type="checkbox" value="#additionals#" onclick="ManageChkBoxes(this);" /></td>
         </tr>
     </table>
</form>

</body>
</html>