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

5 checkboxes only one should be checked..

Status
Not open for further replies.

jagprg

Programmer
Jun 16, 2005
11
US
Hi,

I have five checkboxes out of which only one should be checked. I did the following and it works but very lengthy and redundant.
Code:
if (tornado.checked == true)
    {
        traffic.checked = false;
        cable.checked = false;
        holiday.checked = false;
        outdoor.checked = false;
    }
 if (traffic.checked == true)
    {
        tornado.checked = false;
        cable.checked = false;
        holiday.checked = false;
        outdoor.checked = false;
    }
    .....and so on 5 times



Is there any other simple way this can be achieved..

any ideas/thoughts..

thanks
 
here's a complete example i've shown in the past. just change maxChecked to 1.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]

<html>
<head>
<title>Enabling Certain Amount of Checkboxes</title>

<script language="javascript" type="text/javascript">
<!--

var totalCBs = 0;
var maxChecked = 3;

function keepTrack(c) {
    totalCBs += c.checked ? 1 : -1;
    enableDisable( c.form, totalCBs == maxChecked );
}

function enableDisable(f, b) {
    var e = f.elements;
	for (var i = 0; i < e.length; i++) {
        if (e[i].type == 'checkbox' && !e[i].checked) e[i].disabled = b;
	}
}

-->
</script>

<style type="text/css">

</style>

</head>

<body>

	<form name="f">
		<input type="checkbox" name="cb1" onclick="keepTrack(this);" /> Checkbox 1<br />
		<input type="checkbox" name="cb2" onclick="keepTrack(this);" /> Checkbox 2<br />
		<input type="checkbox" name="cb3" onclick="keepTrack(this);" /> Checkbox 3<br />
		<input type="checkbox" name="cb4" onclick="keepTrack(this);" /> Checkbox 4<br />

		<input type="checkbox" name="cb5" onclick="keepTrack(this);" /> Checkbox 5<br />
		<input type="checkbox" name="cb6" onclick="keepTrack(this);" /> Checkbox 6<br />
		<input type="checkbox" name="cb7" onclick="keepTrack(this);" /> Checkbox 7<br />
	</form>

</body>

</html>

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
why not use radio buttons instead? this is how they are supposed to behave... checkboxes are not meant to behave this way.


-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
ya I know but, business requirement I think at a later stage they say that they might allow user to select more than one check box...

So instead of changing a lot this seems to be a good idea.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top