Solved

Javascript checkbox problem

Posted on 2007-03-20
3
197 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tiny glitch in my main slider 3 40
100% tall div not scrollable on iPhone 3 21
JQuery Syntax... 4 38
Coding for the first time 9 33
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

820 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