Sign in to follow this  
SteveDeFacto

[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 this post


Link to post
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 this post


Link to post
Share on other sites
SteveDeFacto    109
Quote:
Original post by Dragonsoulj
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.


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this