Solved

Javascript checkbox problem

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Need help with this homework problem. 5 78
arrays and radio buttons 10 46
Angular tyeahead feature not working with Control "as" ControlXXX 4 51
HTML editor custom button 3 89
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

752 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