# [web] Check all check boxes when one is checked and then uncheck when it's not.

## Recommended Posts

SteveDeFacto    109
I wrote a javascript function that is suppose to check all check boxes in a list when the main check box is checked then when it's unchecked it unchecks all of the check boxes in the list. The problem is that the code I wrote only seems to check the boxes not uncheck them... I'm calling the function when the onclick event occurs. Here is the source:
<script type="text/javascript">

function CheckAll(chk)
{
if( document.getElementById("all").checked == true )
{
for (i = 0; i < chk.length; i++)
chk[i].checked = false;
}
else
{
for (i = 0; i < chk.length; i++)
chk[i].checked = true;
}
}
</script>



##### Share on other sites
Dragonsoulj    3212
I may be a little bit rusty at javascript but it looks like you have it backwards. When all the elements should be checked it is setting them to false (if .checked == true ... i.checked = false) and when all elements should be unchecked it is setting them to true. Try flipping your true and false assignments.

##### Share on other sites
SteveDeFacto    109
Quote:
 Original post by DragonsouljI may be a little bit rusty at javascript but it looks like you have it backwards. When all the elements should be checked it is setting them to false (if .checked == true ... i.checked = false) and when all elements should be unchecked it is setting them to true. Try flipping your true and false assignments.

Yeah sorry that was for testing, I did find the problem though and it has nothing to do with that function...