Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    1 radio button group imitating other.

    I have 2 radio button groups (A & B) in a form. How can I get a button in group B to automatically be checked based on a button in group A being checked?

    I tried with the script beneath, but with while($row = mysql_fetch_array($result)) php generated form in a HTML table, only the top row in group B changes no mater what row in group A is checked.

    Can anybody help me with this? Thanx!

    Code:
    <script language="javascript">
    function getObject(obj){if(document.getElementById){return document.getElementById(obj);}else{if(document.all){return document.all[obj];}}}
    function radioBtnGroupChanged(obj){
    	switch(obj.id) {
    		case "rbA1":
    			getObject('rbB2').checked = true;
    			break;
    		case "rbA2":
    			getObject('rbB1').checked = true;
    			break;
    	}
    }
    </script>
    
    
    <input type="radio" id="rbA1" name="rbA" onclick="radioBtnGroupChanged(this);" /><br />
    <input type="radio" id="rbA2" name="rbA" onclick="radioBtnGroupChanged(this);" />
    <br />
    <br />
    <br />
    <input type="radio" id="rbB1" name="rbB"  /><br />
    <input type="radio" id="rbB2" name="rbB"  />

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,503
    Thanks
    3
    Thanked 500 Times in 487 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <script language="javascript">
    function radioBtnGroupChanged(obj,to){
     var frm=obj.form;
     for (var z0=0;z0<frm[obj.name].length;z0++){
      if (frm[to][z0]) frm[to][z0].checked=frm[obj.name][z0].checked;
     }
    
    }
    </script>
    
    <form>
    <input type="radio" id="rbA1" name="rbA" onclick="radioBtnGroupChanged(this,'rbB');" /><br />
    <input type="radio" id="rbA2" name="rbA" onclick="radioBtnGroupChanged(this,'rbB');" />
    <br />
    <br />
    <br />
    <input type="radio" id="rbB1" name="rbB"  /><br />
    <input type="radio" id="rbB2" name="rbB"  />
    </form>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    deki (07-17-2009)

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts
    You rock! This works perfectly!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •