Solved

only one checkbox restriction

Posted on 2004-09-15
7
267 Views
Last Modified: 2006-11-17
Hi all,

I have two checkbox input (yes and no);
now it allows user to check/click on both of the inputs;
anybody knows how to allow only one checkbox to be clicked?

current code is something like this:
<INPUT TYPE="CHECKBOX" NAME="CheckBox1" VALUE="1">
<INPUT TYPE="CHECKBOX" NAME="CheckBox2" VALUE="0">

Thanks in advance:D
0
Comment
Question by:wmulyadi
  • 4
  • 2
7 Comments
 
LVL 19

Expert Comment

by:peh803
ID: 12067551
use radio buttons instead...they have this functionality built in.

peh803
0
 
LVL 19

Expert Comment

by:peh803
ID: 12067558
like this:

<INPUT TYPE="radio" NAME="myradio" VALUE="1">
<INPUT TYPE="radio" NAME="myradio" VALUE="0">

regards,
peh803
0
 
LVL 19

Accepted Solution

by:
peh803 earned 500 total points
ID: 12067614
If you REALLY want to use checkboxes, use some javascript like this:

<script type="text/javascript">
  function checkme(obj, id){
    var checkBox1State = cb_1.checked;
    var checkBox2State = cb_2.checked;
    if ((id==1)&&(checkBox1State==true)){
      cb_2.checked=false;
    }
    if ((id==2)&&(checkBox2State==true)){
      cb_1.checked=false;
    }
  }
</script>
<INPUT TYPE="CHECKBOX" NAME="CheckBox1" id="cb_1" VALUE="1" onclick="checkme(this, 1);"><BR>
<INPUT TYPE="CHECKBOX" NAME="CheckBox2" id="cb_2" VALUE="0" onclick="checkme(this, 2);"><BR>

Let me know if you need anything else.
Regards,
peh803
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12067631
If you need to stay with the checkboxes rather than using the radio control, try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
<!--
function setChecks(objCheckControl){
      objForm = document.forms[0];
      if(objCheckControl.checked){
            if(objCheckControl.name=="CheckBox1"){
                  objForm.elements['CheckBox1'].checked = true;
                  objForm.elements['CheckBox2'].checked = false;
            }else{
                  objForm.elements['CheckBox1'].checked = false;
                  objForm.elements['CheckBox2'].checked = true;
            }
      }
}
//-->
</SCRIPT>
</HEAD>

<BODY>
<FORM METHOD=POST ACTION="">
<INPUT TYPE="CHECKBOX" NAME="CheckBox1" VALUE="1" onClick="setChecks(this)">
<INPUT TYPE="CHECKBOX" NAME="CheckBox2" VALUE="0" onClick="setChecks(this)">
</FORM>
</BODY>
</HTML>


FtB
0
 
LVL 19

Expert Comment

by:peh803
ID: 12067660
@FtB:

hee hee, good idea :-)

Great minds think alike!  Altho I do like your js function better (using the name property of the control instead of requiring an additional argument)......

regards,
peh803
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12067696
oops, please ignore my comment as it pretty much duplicates what peh803 posted.

FtB
0
 

Author Comment

by:wmulyadi
ID: 12068718
Thanks all :D
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why Bundles is not working on certain files - ASP MVC 3 66
Question about ASP Including Files 6 64
ASP sending two datas 2 23
Read text on Table 7 29
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question