View Full Version : Checkbox at least one or none [Resolved]
tripwater
11-29-2005, 06:36 PM
Sorry to post such a simple question on here. I did a searcha nd could not find what I was looking for. I basically have 2 checkboxes and I need only one to be checked or niether. I would use radio buttons but if they check one then decide they want none tehn they are screwed.
What I have is a user setup which sets them to Admin, A Manager or if neither is checked, they are just a regular employee.
What I would like to do is if they check admin, then try to check manager, uncheck admin and vice versa. This way they can only have either one or none. I am not very versed in JS so bare with me...
here is what I tried which is obviously wrong.
<javascript>
function only_one()
{
if (document.employees.admin.checked == true)
document.employees.manager.checked != true;
if (document.employees.manger.checked == true)
document.employees.admin.checked != true;
}
</javascript>
<form name="employees">
<input type="checkbox" name="admin" onclick="only_one()">
<input type="checkbox" name="manager" onclick="only_one()">
</form>
Thank you for any help with this.
Pyth007
11-29-2005, 06:48 PM
Use document.employees.admin.checked = false; instead of document.employees.admin.checked != true;
Also, because the checked property is a boolean, you don't necessarily need to use '== true'. Those if's could be written as if (document.employees.admin.checked)
tripwater
11-29-2005, 06:52 PM
Thanks for the reply. I did what you said and what happened was once I checked admin, it refused to let me check manager. WHat I would like for it to do is let me check manager and uncheck admin automatically. SO if one is checked and you check the other it unchecks the checked one and checks the new one...
Pyth007
11-29-2005, 07:23 PM
Yep... sorry about that... didn't check my logic in the script... EDIT DON'T TRY THIS:
function only_one(cBox)
{
// See if box being clicked is already checked
var uncheck=false;
if (cBox.checked)
uncheck=true; // True that it needs to remain 'uncheck'ed
// Uncheck both boxes
document.employees.manger.checked = false;
document.employees.admin.checked = false;
// If the box clicked on does not need to remain unchecked, check it.
if (!uncheck)
cBox.checked=true;
}
</javascript>
<form name="employees">
<input type="checkbox" name="admin" onclick="only_one(this)">
<input type="checkbox" name="manager" onclick="only_one(this)">
</form>
EDIT: Nevermind... it's not working either... hmm... This'll work, however:
<html><body>
<script>
function only_one(cBox)
{
// See if box being clicked gets checked
var alreadyChecked=false;
if (cBox.checked)
alreadyChecked=true; // True that it was alreadyChecked'ed
// Uncheck both boxes
document.getElementById('manager').checked = false;
document.getElementById('admin').checked = false;
// If the box clicked on was alreadyChecked, check it again.
if (alreadyChecked)
cBox.checked=true;
}
</script>
<form name="employees">
<input type="checkbox" id="admin" onclick="only_one(this)">
<input type="checkbox" id="manager" onclick="only_one(this)">
</form>
</body></html>
felgall
11-29-2005, 07:29 PM
Sorry to post such a simple question on here. I did a searcha nd could not find what I was looking for. I basically have 2 checkboxes and I need only one to be checked or niether. I would use radio buttons but if they check one then decide they want none tehn they are screwed.
You could do it with three radio buttons.
Admin
Manager
Neither
tripwater
11-29-2005, 07:31 PM
You could do it with three radio buttons.
Admin
Manager
Neither
I guess I will do that. I tried the updated code Pyth007 posted and I cannot check any of the boxes now. I really do not want to spend any more time on this small prob.
Thanks for everyone's help
Pyth007
11-29-2005, 07:40 PM
Check my reply again... I actually ran my code this time, so the 2nd script should work...
tripwater
11-29-2005, 08:11 PM
Check my reply again... I actually ran my code this time, so the 2nd script should work...
Thanks that revision worked great!!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.