Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

auto check checkboxes

Status
Not open for further replies.

ystm

Programmer
Joined
Nov 28, 2006
Messages
4
Location
GB
Hello all, im trying to create a function that will allow me to auto check checkboxes from another checkbox.

Here is my code:

Code:
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
    <title>Untitled Page</title>
    <SCRIPT LANGUAGE="JavaScript">

function check(field,x) {
        for (i = 0; i < field.length; i++) {
            var id = field[i].id;
            if (field[i].checked == true)
            {
                if (x = "All")
                {
                    field[i].checked = true; 
                }
                else
                {
                    if(id.indexOf(x+'-') != -1){
                        field[i].checked = true; 
                    }
                }  
            }
            else
            {
                if (x = "All")
                {
                    field[i].checked = false; 
                }
                else
                {
                    if(id.indexOf(x+'-') != -1){
                        field[i].checked = false; 
                    }
                }  
            }
        }
}
</script>

</head>
<body>
<form name=myform action="" method=post runat=server>
<table>
<tr><td>
<b>Checkbox Example</b><br>

<input type=checkbox name=list id="1-1" value="1A">A<br>
<input type=checkbox name=list id="1-2" value="2A">A<br>
<input type=checkbox name=list id="2-1" value="3A">A<br>
<input type=checkbox name=list id="2-2" value="4">B<br>
<input type=checkbox name=list id="2-3" value="5">B<br>
<br>            
<input type=checkbox id="Checkbox" onClick="this.value=check(this.form.list,1)">Check All A<br>         
<input type=checkbox id="Checkbox1" onClick="this.value=check(this.form.list,2)">Check All B<br>     
<input type=checkbox id="Checkbox2" onClick="this.value=check(this.form.list,All)">Check All B<br>                               
</td></tr>
</table>
</form>
</body>
</html>

I cant get it to work at the moment, however, i think im very close. Any ideas?
 
Try this script - it is much easier:

<script language="javascript">
function checkAll(){
for (var i=0;i<document.forms[0].elements.length;i++)
{
var e=document.forms[0].elements;
if ((e.name != 'allbox') && (e.type=='checkbox'))
{
e.checked=document.forms[0].allbox.checked;
}
}
}
</script>
<form method="post" actio="checkall.htm">
A <input type="checkbox" name="a">
<br>
B<input type="checkbox" name="b">
<br>
C<input type="checkbox" name="c">
<font face="verdana" size="1">Select all</font>
<input type="checkbox" value="on" name="allbox" onclick="checkAll();"/>
</form>
 
[1] At both instances,
> if (x = "All")
[tt]if (x =[red]=[/red] "All")[/tt]
[2]
> <input type=checkbox id="Checkbox2" onClick="this.value=check(this.form.list,All)">Check All B<br>
[tt]<input type=checkbox id="Checkbox2" onClick="this.value=check(this.form.list,[red]'[/red]All[red]'[/red])">Check All B<br> [/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top