Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need radio buttons to control group of checkboxes 1

Status
Not open for further replies.

jhherring

Programmer
Jun 18, 2002
33
US
First, please let me apologize for choosing "Programmer" to describe myself -- it seemed closest of the available choices, but I am nobody's idea of a programmer.

Second, please let me apologize for the elementary tone and question, but I've searched all over for the answer to this, with no luck. But I'm positive it can be done -- my research has shown me that much.

It should be fairly simple, for a JavaScript guru, that is . . . .

Here's a chance to (1) show off how much you know, (2) help out a newbie who at least has the grace and humility to acknowledge that he's a newbie, and (3) make the world safe for democracy, all in one fell swoop. From poking around in here, it looks like there are quite a few knowledgeable people, so I have faith that my problem will be cheerfully solved about 1 minute after I hit the "Submit Post" button. Extra karma points go out to the first person to solve it (karma points are what I can offer in lieu of cash).


Problem:

I have a form with one set of four radio buttons and one set of 11 checkboxes. It should operate such that if the user checks the first radio button, the group of checkboxes shows one subset checked, and if the user checks another radio button, a different subset of checkboxes is checked, and so on.

The code should, ideally, be alterable so that the exact subset of checkboxes to be checked by clicking a particular radio button is easily changeable. That is, if management decides this week that clicking radio button #2 should produce checked checkboxes #1, #4, and #9, but next week they decide that clicking radio button #2 should produce checked checkboxes #1, #4, #9, AND #10, it shouldn't be a problem to edit the code to bring this about. I would also want to be able to add and delete checkboxes from the set.

The term subset, as used above, should be taken to mean "any one, any group, or all of the available checkboxes."

I bow before the masters, and leave you to it. Thanks very much for your help.

John Herring
jhherring@yahoo.com
 
Hi, John!

Try this...(I'm sure there are better ways :) )
Code:
<html>
<head>
<script language=javascript>
  function doThis(rdo) {
    var list = eval(&quot;document.myform.&quot; + rdo);
    var items = list.value;
    for (var i=0; i < document.myform.elements.length; i++) {
      if (document.myform.elements[i].type == &quot;checkbox&quot;) {
        if (items.indexOf(document.myform.elements[i].value) > -1) {
          document.myform.elements[i].checked = true;
        }
        else
          document.myform.elements[i].checked = false;
      }
    }
  }
</script>
</head>
<form name=myform>
<input type=hidden name=A value=&quot;1,4,7&quot;>
<input type=hidden name=B value=&quot;2,9,10&quot;>
<input type=hidden name=C value=&quot;3,5,6,8&quot;>
<input type=radio name=wr_radio value=&quot;A&quot; onClick=&quot;doThis(this.value);&quot;>Radio A 
<input type=radio name=wr_radio value=&quot;B&quot; onClick=&quot;doThis(this.value);&quot;>Radio B 
<input type=radio name=wr_radio value=&quot;C&quot; onClick=&quot;doThis(this.value);&quot;>Radio C 
<br>
<input type=checkbox name=wc_checkbox value=&quot;1&quot;>Box 1<br>
<input type=checkbox name=wc_checkbox value=&quot;2&quot;>Box 2<br>
<input type=checkbox name=wc_checkbox value=&quot;3&quot;>Box 3<br>
<input type=checkbox name=wc_checkbox value=&quot;4&quot;>Box 4<br>
<input type=checkbox name=wc_checkbox value=&quot;5&quot;>Box 5<br>
<input type=checkbox name=wc_checkbox value=&quot;6&quot;>Box 6<br>
<input type=checkbox name=wc_checkbox value=&quot;7&quot;>Box 7<br>
<input type=checkbox name=wc_checkbox value=&quot;8&quot;>Box 8<br>
<input type=checkbox name=wc_checkbox value=&quot;9&quot;>Box 9<br>
<input type=checkbox name=wc_checkbox value=&quot;10&quot;>Box 10<br>
</form>
</html>
 
This is 99.999999999% perfect! If there's a better way, I'll be amazed to hear it. Thank you, thank you, thank you.

One thing, thought -- when you choose radio button B, checkbox #1 gets checked, even though this is not called for in your code. Am looking into this, and will post the revised code if I can find the bug (but don't count on that).

Any ideas?

Thanks again.

John Herring
jhherring@yahoo.com

 
This is 99.999999999% perfect! If there's a better way, I'll be amazed to hear it. Thank you, thank you, thank you.

One thing, though -- when you choose radio button B, checkbox #1 gets checked, even though this is not called for in your code. Am looking into this, and will post the revised code if I can find the bug (but don't count on that).

Any ideas?

Thanks again.

John Herring
jhherring@yahoo.com

 
Ah, cause the list for B contains 1 and 10.

Try this instead:
Code:
<script language=javascript>
  function doThis(rdo) {
    var list = eval(&quot;document.myform.&quot; + rdo);
    var sitems = list.value;
    var items = sitems.split(&quot;,&quot;);
    for (var i=0; i < document.myform.elements.length; i++) {
      if (document.myform.elements[i].type == &quot;checkbox&quot;) {
        arrayCheck:
        for (var j=0; j < items.length; j++) {
          if (items[j] == document.myform.elements[i].value) {
            document.myform.elements[i].checked = true;
            break arrayCheck;
          }
          else
            document.myform.elements[i].checked = false;
        }
      }
    }
  }
</script>
 
It works! It works! The people rejoice! National holiday declared! Dancing in the streets! Spontaneous celebrations break out all over country!

Thanks, jlsmithprism -- you nailed this one. Since karma points are all I have to offer, I'll vote for you for TipMaster right now.

What a great forum!

John Herring
jhherring@yahoo.com

 
Well, since I can't seem to find the button for voting for you or your reply, and since we gotta do something to express my gratitude . . . . how many karma points you figure your code is worth? (If it's more than 275, I'll have to pay you in installments.)

Thanks again.

John Herring
jhherring@yahoo.com
 
Hey no prob! Glad to be of help. I've found so much useful info here, I like to give back when I can.

Have a great day!
Jessica
 
Thanks again! Turns out when you visit the thread by clicking the link in the e-mail notification that the thread has been updated, there are no links that say &quot;Click here to mark this post as a helpful or expert post!&quot; These only appear if you log in first, which (of course) I wasn't doing. Silly of me. Anyway, you've now been duly voted for.

Thanks again.

John Herring
jhherring@yahoo.com
 
Sure enough, management just changed the requirements -- now there's an additional checkbox that wasn't in the original specs. Using the code whipped up by jlsmithprism yesterday, changing my form to accommodate this request took me about 5 seconds.

Fantastic!

John Herring
jhherring@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top