views:

91

answers:

3

Hi there,

We have a form with a radio button group, where any one of the buttons may already be selected. We want to prevent a user from selecting one of the radio buttons, BUT we want to be able to 'deselect' that radio button by clicking on any other radio. Like so:

Step 1: (initial page load)

  • R1: enabled and clickable
  • R2: disabled, but CHECKED
  • R3: enabled and clickable

Step 2: user clicks R1

  • R1: enabled and CHECKED
  • R2: disabled, NOT CHECKED
  • R3: enabled and clickable

Will the 'disabled' attribute allow this to take place?

+3  A: 

Yes, it will.

Here's a quick example: http://jsbin.com/onuci

Of course, I'm not using any JavaScript for it, but I just wanted to show that a disabled radio button (R2 in the example) can be checked, but then when the user clicks either of the other two buttons, it becomes unchecked and cannot be checked again.

Jeff Rupert
Heh...I should've just tested it myself :-). But hey, you got a couple extra rep points for an easy-peasy (or is it peasie?). Thanks Jeff!
btelles
You're welcome! Glad to be of assistance!
Jeff Rupert
+2  A: 

I suggest using Check boxes for this. What happens if I accidentally activated "R1" and then wanted to deactivate it?

Shoban
Yup, that's part of the use case that I did not explain...Thanks though!
btelles
A: 

jQuery can achieve this quite easily: http://jquery.com/

<html>
<head>
    <script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
    <script type="text/javascript">
     google.load("jquery", "1.3.2");

     $(document).ready(function () {
         $("r1").click(function () { 
          $("r2").removeAttr("checked");
         });
     });
    </script>
</head>
<body>

<form action="form_action.php" method="get">
  <input id="r1" type="radio" name="dog" value="Gus" /> Gus<br />
  <input id="r2" type="radio" name="dog" value="Bruce" checked="checked" disabled="disabled" /> Bruce<br />
  <input id="r3" type="radio" name="dog" value="Champ" /> Champ<br />
  <input type="submit" value="Submit" />
</form>

</body>
</html>
simeonwillbanks
Bah, over engineering on my part.
simeonwillbanks