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 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2003
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Check/Select All?

    Hey is there a code I can use so that if i have a form with check boxes when i click like 'Check All' or something it will select every choice instead of the user having to select every option?

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, there's several ways.

    What would you prefer? A single checkbox that controls the checking/unchecking of all the others? Or a button that says "Check All", then when clicked says "Uncheck All".

    Also, can I see the HTML of your form?
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's one way:

    <script type="text/javascript">
    <!-- Begin
    function checkAll(field)
    {
    for (i = 0; i < field.length; i++)
    field[i].checked = true ;
    }

    function uncheckAll(field)
    {
    for (i = 0; i < field.length; i++)
    field[i].checked = false ;
    }
    // End -->
    </script>
    </HEAD.

    <BODY>
    <form name="myform">
    <input type="checkbox" name="list" value="1">1<br>
    <input type="checkbox" name="list" value="2">2<br>
    <input type="checkbox" name="list" value="3">3<br>
    <input type="checkbox" name="list" value="4">4<br>
    <input type="checkbox" name="list" value="5">5<br>

    <input type="button" name="CheckAll" value="Check All"
    onClick="checkAll(document.myform.list)">
    <input type="button" name="UnCheckAll" value="Uncheck All"
    onClick="uncheckAll(document.myform.list)">
    </form>

    .....Willy

  • #4
    New Coder
    Join Date
    Jul 2003
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey Beetle i wanted it so it could just be another check box that when checked it would check every other box ya know? it could say check all or select all etc...

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, here you go
    Code:
    <html>
    <head>
        <title>Test</title>
    
        <script type="text/javascript">
    
            function checkAll( control, cbGroupName )
            {
                var cbGroup = control.form.elements[cbGroupName], i = 0, cb;
                while( cb = cbGroup[i++] )
                {
                    cb.checked = control.checked;
                }
            }
    
        </script>
    </head>
    <body>
    
        <form>
            <input type="checkbox" onclick="checkAll( this, 'group1' );"/> Check all
            <br/>
            <input type="checkbox" name="group1" value="1"/><br/>
            <input type="checkbox" name="group1" value="2"/><br/>
            <input type="checkbox" name="group1" value="3"/><br/>
            <input type="checkbox" name="group1" value="4"/><br/>
        </form>
    
    </body>
    </html>
    If the group will ever be only 1 checkbox, this function will need some additional code to prevent errors.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”


  •  

    Posting Permissions

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