Automatically select a radio button if a checkbox is selected.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • camdev
    New Member
    • Mar 2008
    • 2

    Automatically select a radio button if a checkbox is selected.

    I have a form with 2 radio buttons and multiple checkboxes (see example below). The one radio button indicates all and the other radio button indicates the user has chosen specific options (checkboxes) from a list. Can I use javascript to automatically select the 2nd radio button if the users clicks on any of the checkboxes?

    <form name=form1>
    <input type=radio name=Radio1 value=All>
    <input type=radio name=Radio1 value=Specific>
    <input type=checkbox name=Checkbox1>
    <input type=checkbox name=Checkbox2>
    <input type=checkbox name=Checkbox3>
    <input type=checkbox name=Checkbox4>
    </form>
  • rnd me
    Recognized Expert Contributor
    • Jun 2007
    • 427

    #2
    Code:
    <form name=form1>
    	<input type=radio name=Radio1 value=All tabindex=1>
    	<input type=radio name=Radio1 value=Specific>
    	<input type=checkbox name=Checkbox1 onclick='sel2()'>
    	<input type=checkbox name=Checkbox2 onclick='sel2()'>
    	<input type=checkbox name=Checkbox3 onclick='sel2()'>
    	<input type=checkbox name=Checkbox4 onclick='sel2()'>
    </form>
    
    
    <script type='text/javascript'>
    	function sel2(){document.forms['form1'].elements['Radio1'][1].focus()
    }
    </script>

    this will not always work in ie, as it allows only the first radio input of a group to be focused via js, when nothing else is selected...

    Comment

    • vee10
      New Member
      • Oct 2006
      • 141

      #3
      Hi ,

      This may solve ur problem

      Code:
      <html xmlns="http://www.w3.org/1999/xhtml" >
      <head runat="server">
          <title>Untitled Page</title>
          <script type='text/javascript'>
          function sel2()
          {
        
         document.form1.Radio1[1].checked = true;
      }
      </script>
      </head>
      <body>        
          <form name="form1" action="" >
          <input type="radio" name="Radio1" value="All" tabindex="1" />
          <input type="radio" name="Radio1" value="Specific" />
          <input type="checkbox" name="Checkbox1" onclick='sel2()' />
          <input type="checkbox" name="Checkbox2" onclick='sel2()' />
          <input type="checkbox" name="Checkbox3" onclick='sel2()' />
          <input type="checkbox" name="Checkbox4" onclick='sel2()' />
      </form>
      </body>
      </html>


      Originally posted by camdev
      I have a form with 2 radio buttons and multiple checkboxes (see example below). The one radio button indicates all and the other radio button indicates the user has chosen specific options (checkboxes) from a list. Can I use javascript to automatically select the 2nd radio button if the users clicks on any of the checkboxes?

      <form name=form1>
      <input type=radio name=Radio1 value=All>
      <input type=radio name=Radio1 value=Specific>
      <input type=checkbox name=Checkbox1>
      <input type=checkbox name=Checkbox2>
      <input type=checkbox name=Checkbox3>
      <input type=checkbox name=Checkbox4>
      </form>

      Comment

      • camdev
        New Member
        • Mar 2008
        • 2

        #4
        Thank you. That's what I needed.

        Originally posted by vee10
        Hi ,

        This may solve ur problem

        Code:
        <html xmlns="http://www.w3.org/1999/xhtml" >
        <head runat="server">
            <title>Untitled Page</title>
            <script type='text/javascript'>
            function sel2()
            {
          
           document.form1.Radio1[1].checked = true;
        }
        </script>
        </head>
        <body>        
            <form name="form1" action="" >
            <input type="radio" name="Radio1" value="All" tabindex="1" />
            <input type="radio" name="Radio1" value="Specific" />
            <input type="checkbox" name="Checkbox1" onclick='sel2()' />
            <input type="checkbox" name="Checkbox2" onclick='sel2()' />
            <input type="checkbox" name="Checkbox3" onclick='sel2()' />
            <input type="checkbox" name="Checkbox4" onclick='sel2()' />
        </form>
        </body>
        </html>

        Comment

        Working...