Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help - Odd count issue

    Hi All,

    Im new here, im not very good at JS and mainly do PHP however i have needed to make a quick script however im not very good at it.

    I am sending a string to permission_groups then spliting this into 4 arrays in JS. when the alert for number comes up it counts correctly 0,1,2,3. I am putting this as the array value to get array text 0,1,2 and 3 however only when it runs the first time and uses 0 it works fine but when it runs the following times i get only the first letter of the array's in the fields "group[number]". I get the following "<option value='o'>o</option>" then the next alert will be "<option value='t'>t</option>" when it should be "<option value='Manager'>Manager</option>". If i do an alert for "group[3]" it works fine and gives me the full text. :S

    Code:
    <script type="text/javascript">
    function changefield(unique_id,userid,active,f_name,l_name,contact_n,contact_e,permission_group_name, permission_groups){
    
    var group=permission_groups.split("_"); 
    
    //alert(group[2]);
    
    count = group.length;
    count--;
    
    number = 0;
    
    while (count>0){
      group = "<option value='"+group[number]+"'>"+group[number]+"</option>";
      count--;
      alert(number);
      alert(group);
      number++;
    }
    
        document.getElementById(unique_id+"user_id").innerHTML = "<input id=\"user_id\" type=\"text\" name='"+unique_id+"_user_id"+"' value='"+userid+"' />";
        document.getElementById(unique_id+"active").innerHTML = "<input id=\"active\" type=\"text\" name='"+unique_id+"_active"+"' value='"+active+"' />";
        document.getElementById(unique_id+"f_name").innerHTML = "<input id=\"f_name\" type=\"text\" name='"+unique_id+"_f_name"+"' value='"+f_name+"' />";
        document.getElementById(unique_id+"l_name").innerHTML = "<input id=\"l_name\" type=\"text\" name='"+unique_id+"_l_name"+"' value='"+l_name+"' />";
        document.getElementById(unique_id+"contact_n").innerHTML = "<input id=\"contact_n\" type=\"text\" name='"+unique_id+"_contact_n"+"' value='"+contact_n+"' />";
        document.getElementById(unique_id+"contact_e").innerHTML = "<input id=\"contact_e\" type=\"text\" name='"+unique_id+"_contact_e"+"' value='"+contact_e+"' />";
        document.getElementById(unique_id+"permission_group_name").innerHTML = "<select name='"+unique_id+"_permission_group_name"+"'>"+group+"<option value='blblb2'>permission set 2</option></select>";
      } // End function changefield
    </script>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    BECAUSE YOUR OWN CODE is *changing* the array group into a STRING on the first time through the loop!!!

    Code:
        group = "<option value='"+group[number]+"'>"+group[number]+"</option>";
    After the first such assignment, group is now a string, and so then when you use group[number] you WILL get ONE CHARACTER.

    Your code is horribly horribly complex, anyway.

    Why would you send a delimited string from PHP instead of just sending an array, to start with?

    And why is your loop so complex?

    Why not simply:
    Code:
    function changefield(unique_id,userid,active,f_name,l_name,contact_n,contact_e,permission_group_name, permission_groups)
    {
        var groupArray =permission_groups.split("_"); 
        var group = "";
        for ( var g = 0; g < groupArray.length; ++g )
        {
            group += '<option value="' + groupArray[number] + '">' + groupArray[number] + '</option>';
        }
        ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Jun 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey,

    thanks that worked a treat , i can see where i went wrong now. Didnt think about the name but all makes sence now.

    Thanks for the post


  •  

    Posting Permissions

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