Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-28-2012, 10:37 PM   PM User | #1
exploit
New Coder

 
Join Date: Jun 2012
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
exploit is an unknown quantity at this point
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>
exploit is offline   Reply With Quote
Old 06-28-2012, 11:25 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 06-29-2012, 08:15 AM   PM User | #3
exploit
New Coder

 
Join Date: Jun 2012
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
exploit is an unknown quantity at this point
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
exploit is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:30 PM.


Advertisement
Log in to turn off these ads.