Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-27-2003, 07:38 PM   PM User | #1
alrukus
New Coder

 
Join Date: Jul 2003
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
alrukus is an unknown quantity at this point
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?
alrukus is offline   Reply With Quote
Old 10-27-2003, 08:15 PM   PM User | #2
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
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”
beetle is offline   Reply With Quote
Old 10-27-2003, 08:19 PM   PM User | #3
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
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
Willy Duitt is offline   Reply With Quote
Old 10-27-2003, 10:31 PM   PM User | #4
alrukus
New Coder

 
Join Date: Jul 2003
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
alrukus is an unknown quantity at this point
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...
alrukus is offline   Reply With Quote
Old 10-27-2003, 11:18 PM   PM User | #5
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
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”
beetle is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:26 AM.


Advertisement
Log in to turn off these ads.