html> <head> <script type="text/javascript"> function abc(f) { var p=document.getElementById("f.group2"); p.value="Yes"; } </script> <title>My Page</title> </head> <body> <form name="f"> <div align="center"><br> <input type="radio" name="group1" onClick="abc(this.form)" value="YES">Yes<br> <input type="radio" name="group1" value="No" checked>No<br> <hr> <input type="radio" name="group2" value="Yes">Yes<br> <input type="radio" name="group2" value="No" checked>No<br> </div> </form> </body> </html> here when the Yes button of radio group1 is selected i want to print the value selected in radio group2...but this code seems not to work properly...plz help me out....
Hi, Is this what your trying to achieve? <html> <head> <script type="text/javascript"> function abc(f) { // get the radio group var radioGroup = document.getElementsByName("group2"); // iterate through the group and find the "Yes" radio button // and select it. var radioLength = radioGroup.length; for(var i = 0; i < radioLength; i++) { radioGroup[i].checked = false; if(radioGroup[i].value == "Yes") { radioGroup[i].checked = true; } } } </script> <title>My Page</title> </head> <body> <form name="f"> <div align="center"><br/> <input type="radio" name="group1" onClick="abc(this.form)" value="Yes">Yes<br> <input type="radio" name="group1" value="No" checked>No<br> <hr> <input type="radio" name="group2" value="Yes">Yes<br> <input type="radio" name="group2" value="No" checked>No<br> </div> </form> </body> </html> Code (markup): The getElementById method your were using returns a single element. getElementsByName will return all the elements with the specified name. We just then iterate through the list of elements and check the one we want. I'll be interested to see what other solutions people come up with. gG