Click to See Complete Forum and Search --> : Using check boxes as radio button
S. Go
12-11-2003, 12:38 AM
My user prefers checkboxes than radio buttons, but wants the checkboxes to act as radio buttons, namely:
1. One of the checkboxes must be checked
2. Only one of the checkboxes can be checked
3. When a checkbox is checked, the previously checked checkbox should be unchecked
4. If a checked checkbox is clicked again, it should remain checked.
Is there a way to do this with Javascript?
Appreciate your response. Thanks in advance.
sg2000
Pittimann
12-11-2003, 12:59 AM
Hi!
Here's some code to play with:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var CheckboxNames = ['checkbox1', 'checkbox2', 'checkbox3', 'checkbox4', 'checkbox5', 'checkbox6'];
var isChecked=0;
function CheckForm(){
for ( var i = 0; i < CheckboxNames.length; i++ ) {
if (document.myForm[CheckboxNames[i]].checked==true) {
isChecked=1;
}
}
if (isChecked!=0){
alert(":-)");
return true;
}
else {
alert("You didn't check a checkbox");
return false;
}
}
function checkOne(CheckboxName) {
for ( var i = 0; i < CheckboxNames.length; i++ ) {
if (CheckboxNames[i]==CheckboxName) document.myForm[CheckboxNames[i]].checked=true;
else document.myForm[CheckboxNames[i]].checked=false;
}
}
//-->
</script>
</head>
<body>
<form name="myForm" onsubmit="return CheckForm()">
<input type="checkbox" name="checkbox1" onclick="checkOne(this.name)"><br>
<input type="checkbox" name="checkbox2" onclick="checkOne(this.name)"><br>
<input type="checkbox" name="checkbox3" onclick="checkOne(this.name)"><br>
<input type="checkbox" name="checkbox4" onclick="checkOne(this.name)"><br>
<input type="checkbox" name="checkbox5" onclick="checkOne(this.name)"><br>
<input type="checkbox" name="checkbox6" onclick="checkOne(this.name)"><br>
<input type="submit" value="Submit"><br>
</form>
</body>
</html>
Cheers - Pit
S. Go
12-11-2003, 09:08 AM
Pit:
Thank you very much for your quick response. It solves my problem. Thanks again.
sg2000