Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkboxed 2

Status
Not open for further replies.

liteon

Programmer
Jan 7, 2003
11
0
0
DE
The script below creates 3 checkboxes. Can anyone please help me to write a script that if checkbox A is pressed then the others (B and C) are disabled, and if checkbox B or/and C are pressed checkbox A will be disabled.

Thanks.

<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<p>A
<input name=&quot;A&quot; type=&quot;checkbox&quot; id=&quot;A&quot; value=&quot;checkbox&quot;>
</p>
<p>B
<input name=&quot;B&quot; type=&quot;checkbox&quot; id=&quot;B&quot; value=&quot;checkbox&quot;>
</p>
<p>C
<input name=&quot;C&quot; type=&quot;checkbox&quot; id=&quot;C&quot; value=&quot;checkbox&quot;>
</p>
</body>
</html>
 
<html>
<head>
<title>Untitled Document</title>
<script>
function oneCheckbox(inBox){
if (inBox.name == &quot;A&quot; && document.myForm.A.checked){
document.myForm.B.checked = false
document.myForm.C.checked = false
}
if (inBox.name == &quot;B&quot; && document.myForm.B.checked){
document.myForm.A.checked = false
document.myForm.C.checked = false
}
if (inBox.name == &quot;C&quot; && document.myForm.C.checked){
document.myForm.A.checked = false
document.myForm.B.checked = false
}
}
</script>
</head>
<body>
<form name=&quot;myForm&quot;>
<p>A
<input name=&quot;A&quot; type=&quot;checkbox&quot; id=&quot;A&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>B
<input name=&quot;B&quot; type=&quot;checkbox&quot; id=&quot;B&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>C
<input name=&quot;C&quot; type=&quot;checkbox&quot; id=&quot;C&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
</form>
</body>
</html> Get the Best Answers! faq333-2924
&quot;A witty saying proves nothing.&quot; - Voltaire
mikewolf@tst-us.com
 
Thanks that&#8217;s what I call JavaScript magic.
 
Is it possible to make the script above generic so a,b,c will be like variables ($a,$b,$c) so I can use the function like oneCheckbox(a,b,c) or oneCheckbox(Tel,Fax,Email)?

<html>
<head>
<title>Untitled Document</title>
<script>
function oneCheckbox(inBox){
if (inBox.name == &quot;A&quot; && document.myForm.A.checked){
document.myForm.B.checked = false
document.myForm.C.checked = false
}
if (inBox.name == &quot;B&quot; && document.myForm.B.checked){
document.myForm.A.checked = false
document.myForm.C.checked = false
}
if (inBox.name == &quot;C&quot; && document.myForm.C.checked){
document.myForm.A.checked = false
document.myForm.B.checked = false
}
}
</script>
</head>
<body>
<form name=&quot;myForm&quot;>
<p>A
<input name=&quot;A&quot; type=&quot;checkbox&quot; id=&quot;A&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>B
<input name=&quot;B&quot; type=&quot;checkbox&quot; id=&quot;B&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>C
<input name=&quot;C&quot; type=&quot;checkbox&quot; id=&quot;C&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
</form>
</body>
</html>
 
Yes. I actually prefer this method:

<html>
<head>
<title>Untitled Document</title>
<script>
function oneCheckbox(inBox){
if (inBox.checked){
boxGroup = inBox.name.substr(0,2)
boxName = inBox.name.substr(2,1)
for (x=0; x<document.myForm.elements.length; x++){
if (document.myForm.elements[x].name.substr(0,2) == boxGroup){
if (document.myForm.elements[x].name.substr(2,1) != boxName){
document.myForm.elements[x].checked = false
}
}
}
}
}
</script>
</head>
<body>
<form name=&quot;myForm&quot;>
<p>A
<input name=&quot;G1A&quot; type=&quot;checkbox&quot; id=&quot;A&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>B
<input name=&quot;G1B&quot; type=&quot;checkbox&quot; id=&quot;B&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>C
<input name=&quot;G1C&quot; type=&quot;checkbox&quot; id=&quot;C&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>2A
<input name=&quot;G2A&quot; type=&quot;checkbox&quot; id=&quot;A&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>2B
<input name=&quot;G2B&quot; type=&quot;checkbox&quot; id=&quot;B&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>2C
<input name=&quot;G2C&quot; type=&quot;checkbox&quot; id=&quot;C&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
</form>
</body>
</html> Get the Best Answers! faq333-2924
&quot;A witty saying proves nothing.&quot; - Voltaire
mikewolf@tst-us.com
 
The last script is above my knowledge, but is it possible to make it behave as shown in the modified script below (it is hard to explain so I think better to see), but change the script to work in an &#8220;a, b, c function&#8221; mode.

<html>
<head>
<title>Untitled Document</title>
<script>
function oneCheckbox(inBox){
if (inBox.name == &quot;A&quot; && document.myForm.A.checked){
document.myForm.B.checked = false
document.myForm.C.checked = false
}
if (inBox.name == &quot;B&quot; && document.myForm.B.checked){
document.myForm.A.checked = false

}
if (inBox.name == &quot;C&quot; && document.myForm.C.checked){
document.myForm.A.checked = false
}
}
</script>
</head>
<body>
<form name=&quot;myForm&quot;>
<p>A
<input name=&quot;A&quot; type=&quot;checkbox&quot; id=&quot;A&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>B
<input name=&quot;B&quot; type=&quot;checkbox&quot; id=&quot;B&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>C
<input name=&quot;C&quot; type=&quot;checkbox&quot; id=&quot;C&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
</form>
</body>
</html>


 
you could use this I have checked it in netscape 7 and ie 5.0

replace the names of the checkboxes


<HTML>
<HEAD>

<script language=&quot;JavaScript&quot;>
function test(passed)
{
window.document.FORM1.checkbox2.disabled =false;
window.document.FORM1.checkbox3.disabled =false;
window.document.FORM1.checkbox3.disabled =false;

if ( passed == 1)
{
if (window.document.FORM1.checkbox1.checked ==false)
{
window.document.FORM1.checkbox2.disabled =false;
window.document.FORM1.checkbox3.disabled =false;
}
else
{
window.document.FORM1.checkbox2.disabled =true;
window.document.FORM1.checkbox3.disabled =true;
}
}
if ( (passed == 2)|| (passed == 3) )
{
window.document.FORM1.checkbox1.disabled =true;

if ( (window.document.FORM1.checkbox2.checked ==false) && (window.document.FORM1.checkbox3.checked == false))
{
window.document.FORM1.checkbox1.disabled =false;
}
else
{
window.document.FORM1.checkbox1.disabled =true;
}
}

}

</script>
<TITLE></TITLE>
</HEAD>
<BODY>
<FORM id=FORM1 name=FORM1 action=&quot;&quot; method=post>
First<INPUT type=&quot;checkbox&quot; id=checkbox1 name=checkbox1 onClick=&quot;javascript: test(1);&quot; >
<br>
Second<INPUT type=&quot;checkbox&quot; id=checkbox2 name=checkbox2 onClick=&quot;javascript: test(2);&quot; >
<br>
Third<BR><INPUT type=&quot;checkbox&quot; id=checkbox3 name=checkbox3 onClick=&quot;javascript: test(3);&quot; >

<P>&nbsp;</P></FORM>

</BODY>
</HTML>
 
You say this is above your knowledge, but let me explain...

<html>
<head>
<title>Untitled Document</title>
<script>
function oneCheckbox(inBox){
if (inBox.checked){
boxGroup = inBox.name.substr(0,2) Holds the first 2 digits of the checkbox's name
boxName = inBox.name.substr(2,1) Holds the A,B,C,D,E (whatever character is in the 3rd slot)
for (x=0; x<document.myForm.elements.length; x++){ look at each element in the form
if (document.myForm.elements[x].name.substr(0,2) == boxGroup){ If the first two chracters match... This enables you to create groups of checkboxes
if (document.myForm.elements[x].name.substr(2,1) != boxName){ if the 3rd chracter doesn't match the 'inBox' then uncheck the box
document.myForm.elements[x].checked = false
}
}
}
}
}
</script>
</head>
<body>
<form name=&quot;myForm&quot;>
<p>A
Now all you have to do is name your boxes with the same first two characters (for boxes in the same group) and a different 3rd character (like a,b,c,d,e,f)
<input name=&quot;G1A&quot; type=&quot;checkbox&quot; id=&quot;A&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>B
<input name=&quot;G1B&quot; type=&quot;checkbox&quot; id=&quot;B&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>C
<input name=&quot;G1C&quot; type=&quot;checkbox&quot; id=&quot;C&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>2A
<input name=&quot;G2A&quot; type=&quot;checkbox&quot; id=&quot;A&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>2B
<input name=&quot;G2B&quot; type=&quot;checkbox&quot; id=&quot;B&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>2C
<input name=&quot;G2C&quot; type=&quot;checkbox&quot; id=&quot;C&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
</form>
</body>
</html> Get the Best Answers! faq333-2924
&quot;A witty saying proves nothing.&quot; - Voltaire
mikewolf@tst-us.com
 
Thanks, view the script it is a bit clearer right now.
It seems that I can only choose one of the boxes while I would thank you if you can modify the code so I can push B, C, D but not A and opposite.

Thanks a lot for all the help.

Script:
<html>
<head>
<title>Untitled Document</title>
<script>
function oneCheckbox(inBox){
if (inBox.checked){
boxGroup = inBox.name.substr(0,2)
boxName = inBox.name.substr(2,1)
for (x=0; x<document.myForm.elements.length; x++){
if (document.myForm.elements[x].name.substr(0,2) == boxGroup){
if (document.myForm.elements[x].name.substr(2,1) != boxName){
document.myForm.elements[x].checked = false
}
}
}
}
}
</script>
</head>
<body>
<form name=&quot;myForm&quot;>
<p>A

<input name=&quot;G1A&quot; type=&quot;checkbox&quot; id=&quot;A&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>B
<input name=&quot;G1B&quot; type=&quot;checkbox&quot; id=&quot;B&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>C
<input name=&quot;G1C&quot; type=&quot;checkbox&quot; id=&quot;C&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>D
<input name=&quot;G1D&quot; type=&quot;checkbox&quot; id=&quot;A&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>

</form>
</body>
</html>
 
Argh! I guess I didn't read closely enough at first... I'll post a solution in a few minutes. Get the Best Answers! faq333-2924
&quot;A witty saying proves nothing.&quot; - Voltaire
mikewolf@tst-us.com
 
I hope this isn't too convoluted....

<html>
<head>
<title>Untitled Document</title>
<script>
function oneCheckbox(inBox){
if (inBox.checked){
boxGroup = inBox.name.substr(0,2)
boxName = inBox.name.substr(2,1)
for (x=0; x<document.myForm.elements.length; x++){
if (document.myForm.elements[x].name.substr(0,2) == boxGroup){
if (boxName == &quot;A&quot;){
if (document.myForm.elements[x].name.substr(2,1) != boxName){
document.myForm.elements[x].checked = false
}
else{
if (document.myForm.elements[x].name.substr(2,1) != &quot;A&quot;){
document.myForm.elements[x].checked = false
}
}
}
else{ // a box other than A was checked
eval(&quot;document.myForm.&quot; + boxGroup + &quot;A.checked = false&quot;)
}
}
}
}
}
</script>
</head>
<body>
<form name=&quot;myForm&quot;>
<p>A

<input name=&quot;G1A&quot; type=&quot;checkbox&quot; id=&quot;A&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>B
<input name=&quot;G1B&quot; type=&quot;checkbox&quot; id=&quot;B&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>C
<input name=&quot;G1C&quot; type=&quot;checkbox&quot; id=&quot;C&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>
<p>D
<input name=&quot;G1D&quot; type=&quot;checkbox&quot; id=&quot;A&quot; value=&quot;checkbox&quot; onClick=&quot;oneCheckbox(this)&quot;>
</p>

</form>
</body>
</html> Get the Best Answers! faq333-2924
&quot;A witty saying proves nothing.&quot; - Voltaire
mikewolf@tst-us.com
 
Thanks a lot, now I need to digest it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top