Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2008
    Posts
    77
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Check another checkbox when a checkbox is checked !

    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 !

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,917
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Like this (but if both boxes must be checked or both unchecked why have two checkboxes?).
    Code:
    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?

    Code:
    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.
    Last edited by Philip M; 06-21-2010 at 05:38 PM.

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts
    This should do it:

    Code:
    //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]));
    	}
    }

  • #4
    New Coder
    Join Date
    Jan 2008
    Posts
    77
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks a lot, it's working!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •