Thursday 1 March 2012

javascript to select all checkboxes by parent checkbox and uncheck parent one if one of the child is unchecked


<script>
window.onload=function(){
            var cbChild = $("[id^='chkOpportunity_']");
                 cbChild.onchange=function(){
                         checkParent();
                  }  
}
$(document).ready(function () {
            checkParent();

          });
        function checkParent() {
            var cbkParent = document.getElementById('chkAll');
            var cbChild = $("[id^='chkOpportunity_']");
            for (var i = 0; i < cbChild.length; i++) {
                if (cbChild[i].checked) {
                    cbkParent.checked = true;
                }
                else {
                    cbkParent.checked = false;
                    break;
                }
            }
        }
</script>
<input type='checkbox' id='chkAll' />

<input type='checkbox' id=' chkOpportunity_1 ' />
<input type='checkbox' id=' chkOpportunity_2 ' />
<input type='checkbox' id=' chkOpportunity_3 ' />
<input type='checkbox' id=' chkOpportunity_4 ' />
<input type='checkbox' id=' chkOpportunity_5 ' />
<input type='checkbox' id=' chkOpportunity_6 ' />


No comments:

Post a Comment