Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

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.
0
lostboyinsofla
Asked:
lostboyinsofla
1 Solution
 
RoonaanCommented:
I would use below function and call it like: onclick="ManageChKBoxes(this);"

function ManageChKBoxes(chkBox) {
  var frm = chkBox.form;
  var name = chkBox.name;
  var head = name.substring(0, name.indexOf('_'));
  var tail    = name.substring(name.indexOf('_'));
  if(chkBox.checked && head.match(/ignore/i)) frm.elements['apply' + tail].checked = false;  
  if(chkBox.checked && head.match(/apply/i)) frm.elements['ignore' + tail].checked = false;  
}

r-
0
 
lostboyinsoflaAuthor Commented:
Too cool.  Thanks for the answer.
0
 
ZvonkoSystems architectCommented:
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>

0
 
thirdCommented:
<!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>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now