Solved

only one checkbox restriction

Posted on 2004-09-15
7
264 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ODBC connection string for ODBC 6.01 24 72
An ASP query I cannot work out... 2 46
Classic ASP - SQL Server connection problem Part 2! 3 53
syntax error on ASP ? 6 43
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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/…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now