Solved

Javascript checkbox problem

Posted on 2007-03-20
3
202 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
[X]
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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
Suggested Courses

635 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