Wednesday 2 April 2014

Check if a checkbox is checked in CheckboxList with javascript

Check if a checkbox is checked in CheckboxList with javascript


Description:
                  Find if the checkbox in the Checkboxlist is checked and has the text none or n/a and perform functionality in javascript.

ASPX:

<asp:CheckBoxList runat="server" ID="chkList" TabIndex="21" onclick="chkList_click();">
</asp:CheckBoxList>

JAVASCRIPT:

function chkList_click() {        
     // Get CheckboxList element        
     var elementRef = document.getElementById("<%=chkList.ClientID%>");          
     // Get input element inside the CheckboxList         
     var checkBoxArray = elementRef.getElementsByTagName('input');        
     // Iterate throught the CheckboxList input tag array        
     for (var i = 0; i < checkBoxArray.length; i++) {            
           // Get every checkbox by loop            
           var listControl = checkBoxArray[i];            
           if (listControl.type == "checkbox") { // Check if Checkbox                
                  if (listControl.checked == true) { // if checked true                   
                         //find Checkbox text.                    
                         var labelArray = listControl.parentElement.getElementsByTagName('label');                    
                         if (labelArray[0].innerHTML.toLowerCase() == 'none' ||                                                                  labelArray[0].innerHTML.toLowerCase().indexOf('n/a')  > -1) {                        
                                 // Your code here                        
                                 //checked = true;                        
                                 //break;                    
                         }                
                   }            
            }        
      } 
}

1 comment:


  1. It is really very helpful for us and I have gathered some important information from this blog.
    Yii Framework Development Company – Nintriva

    ReplyDelete