Check uncheck checkbox

I have few checkboxes , out of which one is “Select All” and the others are some standalone’s.

The requirement is that

a) Ifthe user click on "Select All" checkbox, then all other checkboxes will be checked.
b) If he unchecks the "Select All" checkbox , then everything will be unchecked.
c) And if any one of the individual checkbox is unchecked, then the "Select all" will be unchecked.
d)If all the four check boxes are checked, then the "Select All" will be checked.

I am using JQuery. I am able to achieve the first two but not the last two. My code is as under

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> Check / UnCheck All Checkbox </TITLE> 

  <script  type="text/javascript" src="jquery.js"></script>

  <script type="text/javascript">

  function SelectDeselect()
  {
        if(document.getElementById('chkSelectDeselectAll').checked)  $  ("INPUT[type='checkbox']").attr('checked',true); 

        else  $  ("INPUT[type='checkbox']").attr('checked',false);
  }

  </script>

 </HEAD>

 <BODY>

    <input type="checkbox" id="chkSelectDeselectAll" onClick="SelectDeselect()">Select All
    <br><br>

    Select your hobbies:
    <br><br>

    <input type="checkbox" id="chkTV">Watching T.V.<br><br>

    <input type="checkbox" id="chkGardening">Gardening<br><br>

    <input type="checkbox" id="chkSwimming">Swimming<br><br>

    <input type="checkbox" id="chkChess">Playing Chess<br><br>

 </BODY>
</HTML>

Need help to implement the rest.

Thanks

newest questions tagged jquery – Stack Overflow

About Admin