Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Want to only tick one box

Status
Not open for further replies.

meeble3

Programmer
Nov 8, 2004
52
GB
Hello

I have a form with three tick boxes.

I want the user to be able to tick one only.

How do I do this please?

Thanks

James
 
Use radio buttons that all have the same name, just different values. Only one can be chosen from the group that way.

Lee
 
No has to be tick boxes, not radio buttons, I'm afraid...
 
Why? What you described is best done with radio buttons. It CAN be done with checkboxes, but isn't as nice and neat.

Lee
 
Doing it for someone who likes ticking boxes :(
 
name all your checkboxes the same thing then incorporate this code into your page:
Code:
<script language=javascript>
function emulateRadio(obj) {
   checkGroup = document.forms["blahForm"].elements["chkgrp"];
   for (i = 0; i < checkGroup.length; i++) {
      if (checkGroup[i] != obj) {
         checkGroup[i].checked = false;
      }
   }
}
</script>
<body>
<form name=blahForm>
<input type=checkbox name=chkgrp onclick='emulateRadio(this)'>Your Friend<br>
<input type=checkbox name=chkgrp onclick='emulateRadio(this)'>Should Learn<br>
<input type=checkbox name=chkgrp onclick='emulateRadio(this)'>To Like<br>
<input type=checkbox name=chkgrp onclick='emulateRadio(this)'>Radio Buttons<br>
</form>
</body>

As an aside, trollacious is completely right. Radio buttons inherently have this behavior. Any user that comes to your page seeing checkboxes is going to assume that they can have more than one checked at a time. Radio buttons suggest to the user that only one can be checked. It may seem a bit confusing for new users to your site to see checkboxes become unchecked when they start checking others.

-kaht

Weaseling out of things is important to learn. It's what separates us from the animals... except the weasel. - Homer Simpson (no, I'm not Homer)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top