Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Checkboxs act like radio buttons

Posted on 2006-07-18
4
193 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
ID: 17133510
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
ID: 17133593
Too cool.  Thanks for the answer.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17133663
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
ID: 17133747
<!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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
key press alert 2 32
suddenly angular cli doesn't work. 5 38
HTML Anchor Link Problem 3 35
Button on Table, name table1 not working 4 20
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 …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

789 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