Solved

Checkboxs act like radio buttons

Posted on 2006-07-18
4
188 Views
Last Modified: 2008-02-01
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
Comment
Question by:lostboyinsofla
4 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
Comment Utility
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
 

Author Comment

by:lostboyinsofla
Comment Utility
Too cool.  Thanks for the answer.
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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
 
LVL 30

Expert Comment

by:third
Comment Utility
<!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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now