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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkboxes with Radio Group behaviour (almost)

Status
Not open for further replies.

Alski1000

Technical User
Nov 13, 2006
10
GB
Hi,

4 checkboxes labelled 1-4 within the same form. All start off unchecked.

The first 3 checkboxes act like a radio group when checked, but checkbox 4 is independent from the 'radio group' and needs to act like a regular checkbox.

I have been using the following script:

function Boxcheck(obj)
{
with(obj.form)
for(i=0;i<elements.length;i++)
if(elements.type=="checkbox" && elements!=obj)
elements.checked=false;
}


and the checkboxes with the code in the format of:

<input name="STR" type="checkbox" id="STR" value="1" onclick="Boxcheck(this)">


but this chnages every checkbox including checkbox 4, which I don't want it to. Any ideas?


Cheers :)




 
there are a few ways of doing this. one way would be to use a naming convention for the three related. something like "cbg_opt1", "cbg_opt2", and "cbg_opt3". the fourth checkbox, name something not starting with "cbg_", maybe "other_opt".

then, you could test the name:

Code:
function Boxcheck(obj)
{
     with(obj.form) {
         for(i=0;i<elements.length;i++) {
         if(elements[i].type=="checkbox" && elements[i].name.indexOf("cbg_") != 0)
             elements[i].checked=false;
         }
     }
}

consequently, i don't see how your code worked without the necessary curly braces...



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Looks like you're looping through all the checkbox form elements -including the last = fourth.

Think the loop should look like this:
Code:
for(i=0;i<(elements.length-1);i++)
 
Hi cLFlaVA & dkdude - thank you both for responding.

dkdude's method worked although my final form will be more complicated (with several more non-radio checkboxes) which is why I went with cLFlaVA' method.

However, it looks as though something is missing as it doesn't work. Try the following code out:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" [ignore]"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"[/URL][/ignore]>
<html [ignore]"xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL][/ignore]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

<script language="JavaScript" type="text/JavaScript">
function Boxcheck(obj)
{
     with(obj.form) {
         for(i=0;i<elements.length;i++) {
         if(elements[i].type=="checkbox" && elements[i].name.indexOf("cbg_") != 0)
             elements[i].checked=false;
         }
     }
}

</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<input name="cbg_1" type="checkbox" id="cbg_1" value="1" onclick="Boxcheck(this)"/>
<input name="cbg_2" type="checkbox" id="cbg_2" value="2" onclick="Boxcheck(this)"/>
<input name="cbg_3" type="checkbox" id="cbg_3" value="3" onclick="Boxcheck(this)"/>
<input name="other_1" type="checkbox" id="other_1" value="4" onclick="Boxcheck(this)"/>	  
</form>
</body>
</html>
 
sorry man... change your if-statement to this (should be "equals", not "does not equal" and i left out your second check...

Code:
if(elements[i].type=="checkbox" && elements[i].name.indexOf("cbg_") == 0 && elements[i].name!=obj.name)

p.s. - are you sure you want that onclick call in your last checkbox?



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
This works :)

Thank you so much. And taken care of in 91 minutes !!!

P.S.
You were right - don't need the final onclick call
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top