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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    The Netherlands
    Posts
    252
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Select radio button with onclick event

    Hi there,

    I was wondering how one can select a radio button when a certain checkbox is clicked. I've got two radio buttons (one with value 1 and the other with value 2). Furthermore I've got three checkboxes that are positioned in a way that they appear as subitems for the second radio button. Now I would like to enhance that effect by selecting the second radio button whenever one of the checkboxes are clicked.

    How does one do this? I already tried assigning a value to the radio button with an onclick event (onclick="document.form.B2.value = 2"), but this doesn't seem to work.

    Anyone know how to do this?

    Thanx, Michiel

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Please try the following


    <script>
    <!--

    function chkme(){
    count=0
    for(i=0;i<3;i++){
    if(document.myform.elements["c"+i].checked==true){
    document.myform.r1[1].checked=true
    }
    else{
    count++
    }
    }
    if(count==3){
    document.myform.r1[0].checked=true
    }
    }

    //-->
    </script>

    <form name="myform" >
    <input type="radio" name="r1">
    <input type="radio" name="r1">

    <input type="checkbox" name="c0" onclick="chkme()">
    <input type="checkbox" name="c1" onclick="chkme()">
    <input type="checkbox" name="c2" onclick="chkme()">
    </form>
    Last edited by Mr J; 01-17-2005 at 07:28 PM. Reason: Corrected typo
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Edinburgh, UK
    Posts
    402
    Thanks
    2
    Thanked 1 Time in 1 Post
    Alternatively you could do it this way:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    </head>
    
    <body>
    <form name="form1" method="post" action="">
      <p>
        <label>
        <input name="RadioGroup1" type="radio" value="1" checked>
      Radio</label>
        <br>
        <label>
        <input type="radio" name="RadioGroup1" value="2">
      Radio</label>
        <br>
      </p><label><input name="box1" type="checkbox" onclick="document.form1.box1.checked == true?document.form1.RadioGroup1[1].checked=true:document.form1.RadioGroup1[0].checked=true;" value=""> 
      Testing...</label>
    </form>
    </body>
    </html>
    Rich

    "An expert is a person who has made all the mistakes that can be made in a very narrow field."

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Badman3k

    I think you forgot to take into account that there is going to be 3 checkboxes
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Edinburgh, UK
    Posts
    402
    Thanks
    2
    Thanked 1 Time in 1 Post
    That's a good point, I did neglect to consider the possibility that although one may be being unselected this doesn't mean that others are not selected.

    Thanks Mr J
    Rich

    "An expert is a person who has made all the mistakes that can be made in a very narrow field."


  •  

    Posting Permissions

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