[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

only one checkbox restriction

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
wmulyadi
Asked:
wmulyadi
  • 4
  • 2
1 Solution
 
peh803Commented:
use radio buttons instead...they have this functionality built in.

peh803
0
 
peh803Commented:
like this:

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

regards,
peh803
0
 
peh803Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
fritz_the_blankCommented:
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
 
peh803Commented:
@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
 
fritz_the_blankCommented:
oops, please ignore my comment as it pretty much duplicates what peh803 posted.

FtB
0
 
wmulyadiAuthor Commented:
Thanks all :D
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now