Solved

Javascript checkbox problem

Posted on 2007-03-20
3
192 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
Comment Utility
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
Comment Utility
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
Comment Utility
That works great, thanks!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

762 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

8 Experts available now in Live!

Get 1:1 Help Now