SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    CheckBoxList in JavaScript Problem

    I have an asp:CheckBoxList named cblRoles with values. The values are coming from the database and can be in any order in cblRoles. When I check enabled “Admin” option from cblRoles my JavaScript function CheckAdmin’s IF STATEMENT works but when I check enabled “Admin” option from cblRoles and make “Admin” option DISABLE, listControl.checked in the IF STATEMENT FAILS. I want my IF STATEMENT code to execute even when I check "Admin" option and make it disbaled.

    Here is my complete JavaScript code:
    Code:
    function CheckAdmin(source, args) 
    {    
          args.IsValid = false;           
          var cblRoles = document.getElementById('<%= cblRoles.ClientID %>');                  
           var num = cblRoles.childNodes[0].childNodes.length;      
           var currentTable = cblRoles.childNodes[0];                  
    
          for (var i=0 ; i<num ; i++)     
          {        var InnetText = currentTable.childNodes[i].childNodes[0].innerText;                
                    var currentTd = currentTable.childNodes[i].childNodes[0];                 
                    var listControl = currentTd.childNodes[0];                                       
                    if (InnetText=="Admin" && listControl.checked)       
                    {                         
                      args.IsValid = true;  return;      
                    }
          } 
    }
    
    Here I am posting my generated SOURCE code 
    
    Code HTML4Strict:
     
     
     
    <tr> 
    <td><span disabled="disabled"><input id="ctl00_ContentPlaceHolder1_cblRoles_0" type="checkbox" name="ctl00$ContentPlaceHolder1$cblRoles$0" checked="checked" disabled="disabled" /><label for="ctl00_ContentPlaceHolder1_cblRoles_0">Admin</label></span>
     
    </td> 
    </tr>
     
    <tr> 
    <td><input id="ctl00_ContentPlaceHolder1_cblRoles_1" type="checkbox" name="ctl00$ContentPlaceHolder1$cblRoles$1" /><label for="ctl00_ContentPlaceHolder1_cblRoles_1">Electrichain</label>
     
    </td> 
    </tr>
     
    <tr> 
    <td><input id="ctl00_ContentPlaceHolder1_cblRoles_2" type="checkbox" name="ctl00$ContentPlaceHolder1$cblRoles$2" /><label for="ctl00_ContentPlaceHolder1_cblRoles_2">Painter</label>
     
    </td> 
    </tr>
     
    <tr><td><input id="ctl00_ContentPlaceHolder1_cblRoles_3" type="checkbox" name="ctl00$ContentPlaceHolder1$cblRoles$3" /><label for="ctl00_ContentPlaceHolder1_cblRoles_3">Tenant</label></td> 
    </tr>

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    England
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you allow, I would like to make some observations, instead of actually solving your problem. I know you would prefer me to just go da-da and problem solved.

    1. Your code is not gonna run on different browers, you've made some horrible consts, that will break. Don't presume innerText will be in childNode[0], there could be 3 of them and the text is in number 2. If I read your error correctly, you can't read the value from a disabled element/node, well you could always, enable it, read it then disable it again afterwards.

    2. This is personal, but a list of check boxes is not a good design; use either:- A multi select list box or the one I prefer two list boxes side to side with add, remove buttons in between. Why ?, because I presume at some point, you'll have a list of 50 checkboxes under some condition and well, that just looks ugly.
    I know its common to see lists of check boxes, but they should be for unrelated stuff, if the stuff is related, in this case roles/occupations, use another type of control that better expresses what you want the user to do, especially when this list of roles/occupations is of variable length.

    Sorry for bringing up stuff, you didn't ask advice for, I know it can be irratating.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SirKilljoy View Post
    If you allow, I would like to make some observations, instead of actually solving your problem. I know you would prefer me to just go da-da and problem solved.

    1. Your code is not gonna run on different browers, you've made some horrible consts, that will break. Don't presume innerText will be in childNode[0], there could be 3 of them and the text is in number 2. If I read your error correctly, you can't read the value from a disabled element/node, well you could always, enable it, read it then disable it again afterwards.

    2. This is personal, but a list of check boxes is not a good design; use either:- A multi select list box or the one I prefer two list boxes side to side with add, remove buttons in between. Why ?, because I presume at some point, you'll have a list of 50 checkboxes under some condition and well, that just looks ugly.
    I know its common to see lists of check boxes, but they should be for unrelated stuff, if the stuff is related, in this case roles/occupations, use another type of control that better expresses what you want the user to do, especially when this list of roles/occupations is of variable length.

    Sorry for bringing up stuff, you didn't ask advice for, I know it can be irratating.
    Thank you very much for you reply and advice. I take your advice and for the next time, I will avoid using CHECKBOXLIST.

    BUT how I can fix my code this time using CHECKBOXLIST and for different browsers.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •