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

Started by
1 comment, last by JackOfAllTrades 14 years, 1 month ago
I wrote a &#106avascript 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.checked = false;
  }
  else
  {
    for (i = 0; i < chk.length; i++)
    chk.checked = true;
  }
}
</script>

Advertisement
I may be a little bit rusty at &#106avascript 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.
Quote:Original post by Dragonsoulj
I may be a little bit rusty at &#106avascript 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.<!--QUOTE--></td></tr></table></BLOCKQUOTE><!--/QUOTE--><!--ENDQUOTE--><br><br>Yeah sorry that was for testing, I did find the problem though and it has nothing to do with that function...

This topic is closed to new replies.

Advertisement