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 to the CF scene
    Join Date
    Dec 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    checking a checkbox from a group using javascript

    hi all,
    I have a group of checkboxes assigned with a same name dynamically. like this i have different groups. Also i have a checkbox for each group. if click on this checkbox all the checkboxes in this group are to be checked. how can i do this.
    i would apprieciate if any one provide me a solution for this.
    thanks
    Murali

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    loop through them, and, if each invidual array item (ie checkbox) is not checked, then check it.
    *keep it simple (TM)

  • #3
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    this should help you out...
    Code:
    <html>
    <head>
    <script language="javascript">
    function checkAll()
    {
    	for(var i = 0; i < document.forms[0].check.length; i++)
    	{
    		if(document.forms[0].check1.checked)
    		{
    			document.forms[0].check[i].checked = true;
    		}
    		else
    		{
    			document.forms[0].check[i].checked = false;
    		}
    	}
    }
    
    </script>
    <body>
    <form>
    <input type="checkbox" name="check1" onclick="checkAll();"><br>
    <input type="checkbox" name="check"><br>
    <input type="checkbox" name="check"><br>
    <input type="checkbox" name="check"><br>
    <input type="checkbox" name="check"><br>
    <input type="checkbox" name="check"><br>
    <input type="checkbox" name="check"><br>
    <input type="checkbox" name="check"><br>
    </form>
    </body>
    </html>

  • #4
    Regular Coder
    Join Date
    Mar 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can shorten that and make it faster with a local variable.
    Code:
    for(var i = 0, form= document.forms[0]; i < form.check.length; i++) {
    	form.check[i].checked = form.check1.checked;


  •  

    Posting Permissions

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