...

View Full Version : Check another checkbox when a checkbox is checked !



ungovernable
06-21-2010, 06:12 PM
So here's what i want to do:

i have 2 checkboxes,

when Checkbox A is checked, i want to automatically check the checkbox B. When A is unchecked, then uncheck B

how can i do that?

thanks a lot !

Philip M
06-21-2010, 06:31 PM
Like this (but if both boxes must be checked or both unchecked why have two checkboxes?).

A <input type = "checkbox" id = "chkA" onclick = "chkIt()">
B <input type = "checkbox" id = "chkB" onclick = "chkIt()">

<script type = "text/javascript">
function chkIt() {
if (document.getElementById("chkA").checked) {
document.getElementById("chkB").checked = true;
}
if (!document.getElementById("chkA").checked) {
document.getElementById("chkB").checked = false;
}
}
</script>

Or can B be checked when A is not?


A <input type = "checkbox" id = "chkA" onclick = "chkItA()">
B <input type = "checkbox" id = "chkB" onclick = "chkItB()">

<script type = "text/javascript">
function chkItA() {
if (document.getElementById("chkA").checked) {
document.getElementById("chkB").checked = true;
}
if (!document.getElementById("chkA").checked) {
document.getElementById("chkB").checked = false;
}
}

function chkItB() {
if (document.getElementById("chkA").checked) {
document.getElementById("chkB").checked = true;
}
}
</script>"I find television very educating. The minute somebody turns it on, I go into the library and read a book." - Groucho Marx, 1890-1977.

gusblake
06-21-2010, 06:34 PM
This should do it:



//edit the connections here.
//keys are the id of the controlling checkbox, values are the ids of the ones to be automatically toggled

var checkconnect={
"a": "b"/*,
"c": "d"*/
}

function connect(a, b) {
return function() {
b.checked=a.checked;
}
}

window.onload=function() {
for(var n in checkconnect) {
a.onclick=connect(document.getElementById(n), document.getElementById(checkconnect[n]));
}
}

ungovernable
06-21-2010, 06:55 PM
thanks a lot, it's working!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum