Solved

only one checkbox restriction

Posted on 2004-09-15
7
263 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Remove all occurances from js string 12 51
Visual Project Bridge error 5 56
Syntax Help on SP 4 52
Group by correlation 4 43
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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/…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

22 Experts available now in Live!

Get 1:1 Help Now