tags:

views:

26

answers:

3

how do i get the user to check only one of these checkboxes .. is there any way other than disabling the checkboxes on selection??

<td width="25%"><input type="checkbox" name="choice" value="Good" />Good</td>
<td width="25%"><input type="checkbox" name="choice" value="Average" />Average</td>
<td width="25%"><input type="checkbox" name="choice" value="Sucks" />Sucks</td>
<td width="25%"><input type="checkbox" name="choice" value="Fascinating" />Fascinating</td>

if its a javascript, please make it simple to understand ..

+5  A: 

You are looking for a radio button group.

<td width="25%"><input type="radio" name="choice" value="Good" />Good</td>
<td width="25%"><input type="radio" name="choice" value="Average" />Average</td>
<td width="25%"><input type="radio" name="choice" value="Sucks" />Sucks</td>
<td width="25%"><input type="radio" name="choice" value="Fascinating" />Fascinating</td>
Pekka
+1, darnit! Beat me by 3 seconds :-P Here's an example for the OP: http://jsfiddle.net/avXs3/
Andy E
@Andy I was going to reply that I was faster because I actually have *hands* to type with, but then I looked at your user name. So glad to see you have your body back! :)
Pekka
@Pekka: what, this old thing? Yeah, it's not mine I'm just borrowing it ;)
Andy E
@Andy Awww. NHS issue replacement body? I *keep* telling people you need private insurance for a body transplant! But it's so hard to get people to buy insurance when everything's still attached.
Pekka
@Pekka: yeah, luckily they got it right second time. First attempt they put it on backwards.
Andy E
@Andy Phew. I'm glad it worked out!
Pekka
A: 

If the user can only select one of them at a time, you should use <input type="radio"> instead of checkbox.

If you must use checkbox, try (example):

var choices = document.getElementsByName('choice');

for (var i = choices.length-1; i >= 0; -- i) {
  choices[i].onclick = function () {
    for (var j = choices.length-1; j >= 0; -- j) {
      if (choices[j] != this)
        choices[j].checked = false;
    }
  }
}
KennyTM
A: 

Use radio buttons instead for your requirement. When you select, only one of them will get checked.

Here is the working demo of that :)

Sarfraz