Solved

Javascript checkbox problem

Posted on 2007-03-20
3
195 Views
Last Modified: 2008-01-09
I have the following javascript to make checkboxes act like radio boxes (allow only 1 to be checked) which doesn't work. The condvals array contains the variable names:

function checkBoxValidate(cb) {
      var condvals=new Array("document.form1.conditioncancerBladder","document.form1.conditioncancerBrain","document.form1.conditioncancerBreast","document.form1.conditioncancerColorectal","document.form1.conditioncancerEndometrial","document.form1.conditioncancerEsophageal","document.form1.conditioncancerKidney","document.form1.conditioncancerLeukemia","document.form1.conditioncancerLung","document.form1.conditioncancerMelanoma","document.form1.conditioncancerNonhodgkinsLymphoma","document.form1.conditioncancerPancreatic","document.form1.conditioncancerProstate","document.form1.conditioncancerStomach","document.form1.conditioncancerThyroid","document.form1.conditioncancerOther");
      for (j = 0; j < 16; j++) {
            if (eval(condvals[j] +".checked") == true) {
                  condvals[j].checked = false;
                  if (j == cb) {
                        condvals[j].checked = true;
                  }
            }
       }
}

The input tags have
onClick="javascript:checkBoxValidate(0)"
onClick="javascript:checkBoxValidate(1)"
etc.

It produces no errors but doesn't work.
Thanks
0
Comment
Question by:burnsj2
  • 2
3 Comments
 
LVL 15

Expert Comment

by:Thogek
ID: 18762031
It's a little cryptic-looking, but how about something like:

    function checkBoxValidate(cb) {
        var condvals=new Array("document.form1.conditioncancerBladder","document.form1.conditioncancerBrain","document.form1.conditioncancerBreast","document.form1.conditioncancerColorectal","document.form1.conditioncancerEndometrial","document.form1.conditioncancerEsophageal","document.form1.conditioncancerKidney","document.form1.conditioncancerLeukemia","document.form1.conditioncancerLung","document.form1.conditioncancerMelanoma","document.form1.conditioncancerNonhodgkinsLymphoma","document.form1.conditioncancerPancreatic","document.form1.conditioncancerProstate","document.form1.conditioncancerStomach","document.form1.conditioncancerThyroid","document.form1.conditioncancerOther");

        oClicked = eval(condvals[cb])
        for (j = 0; j < 16; j++) {
            oJ = eval(condvals[j]);
            oJ.checked = ( (j == cb) && oClicked.checked );
        }
    }
0
 
LVL 15

Accepted Solution

by:
Thogek earned 500 total points
ID: 18762050
Or, perhaps a bit simpler...

    function checkBoxValidate(oChecked) {
        var theForm = oChecked.form;
        for(i = 0;  i < theForm.elements.length;  i++) {
            oElem = theForm.elements[i];
            if(oElem.type == "checkbox") {
                oElem.checked = ( (oElem == oChecked)  &&  oChecked.checked )
            }
        }
    }

with the checkboxes all adjusted to:

    <input type="checkbox" name="conditioncancerBladder" onclick="checkBoxValidate(this)" />Bladder<br />
    <input type="checkbox" name="conditioncancerBrain" onclick="checkBoxValidate(this)" />Brain<br />
    <input type="checkbox" name="conditioncancerBreast" onclick="checkBoxValidate(this)" />Breast<br />
Etc.
0
 
LVL 1

Author Comment

by:burnsj2
ID: 18762113
That works great, thanks!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

831 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