?
Solved

only one checkbox restriction

Posted on 2004-09-15
7
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
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.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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/…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 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