Abel Jojo
Abel Jojo

Reputation: 758

javascript loop variable substituion

I need to use the javascript loop variable i in the loop block.

    for (i=0; i<=4; i++)
        {
            status=!status; 
            document.Form_Users.DefaultUser1_UserName.disabled = status;
            document.Form_Users.DefaultUser1_Password.disabled = status;
        }

what i need is :

       document.Form_Users.DefaultUser{i}_UserName.disabled = status;

ie, I could substitute the value of i insted of 1.

I thank all of you in advance.

Upvotes: 0

Views: 136

Answers (4)

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14282

Try this:

for (i=1; i<5; i++)
    {
        status=!status; 
        document.Form_Users['DefaultUser'+i+'_UserName'].disabled = status;
        document.Form_Users['DefaultUser'+i+'_Password'].disabled = status;
    }

The loop starts with 1 and ends after 4.

Upvotes: 1

mplungjan
mplungjan

Reputation: 177721

Suggestion:

Give them all the same name like
DefaultUser_UserName[]
DefaultUser_Password[]

then you can look over document.Form_Users["DefaultUser_UserName[]"]

and if you use PHP on the backend, you even get the array for free

function enable_text(status)
  var defaultUsers = document.Form_Users["DefaultUser_UserName[]"];
  var defaultUsers = document.Form_Users["DefaultUser_Password[]"];
  status=!status; 
  for (var i=0, n=defaultUsers.length; i<n; i++) {
    defaultUsers[i].disabled = status;
    defaultPass[i].disabled = status;
  }
}

Upvotes: 0

Art
Art

Reputation: 5924

You can do it like so:

document.Form_Users["DefaultUser" + i + "_UserName"].disabled = status;
document.Form_Users["DefaultUser" + i + "_Password"].disabled = status;

Upvotes: 0

dhh
dhh

Reputation: 4337

Have you tried out

document.Form_Users["DefaultUser"+i+"_UserName"]["disabled"] = status;

Upvotes: 0

Related Questions