Hi
I’m trying to write a js thing that will check or uncheck groups of checkboxes and I’m not doing very well – I think there’s something basic that i haven’t understood about when checkboxes are checked and when they’re not
here’s my code so far
[code=php]<html>
<head>
</head>
<body>
<form name=f1>
<input type=”checkbox” name=”a1″ value=”1″>a1<br />
<input type=”checkbox” name=”a2″ value=”2″>a2<br />
<input type=”checkbox” name=”a3″ value=”3″>a3<br />
<input type=”checkbox” name=”a4″ value=”4″>a4<br />
<input type=”checkbox” name=”a5″ value=”5″>a5<br />
<br />
<input type=”checkbox” name=”a” value=”5″ onclick=”selectAll(this.value,this.name)”>Check/uncheck all<br />
<br />
<input type=”checkbox” name=”b1″ value=”1″>b1<br />
<input type=”checkbox” name=”b2″ value=”2″>b2<br />
<input type=”checkbox” name=”b3″ value=”3″>b3<br />
<input type=”checkbox” name=”b4″ value=”4″>b4<br />
<br />
<input type=”checkbox” name=”b” value=”4″ onclick=”selectAll(this.value,this.name)”>Check/uncheck all<br />
<br />
</form>
<script>
function selectAll(val,name){
if(document.f1[‘name’].checked=true){
alert(name + ” checked”);
for(var i=1;i<val;i++){
document.f1[‘name’+i].checked=true;
}
}else{
alert(name + ” unchecked”);
for(var i=1;i<val;i++){
document.f1[‘name’+i].checked=false;
}
}
}
</script>
</body>
</html>
the alert(name + ” checked”); part is triggered when i check and when i uncheck one of the ‘master’ checkboxes so there’s definitely something wrong here
has anyone got any ideas ?
thanks