0

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.

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, But is doen't produce what i need. Your code output such as function enable_text(status) { for (i=0; i<=4; i++) { status=!status; document.Form_Users["DefaultUser" + i + "_UserName"].disabled = status; document.Form_Users["DefaultUser" + i + "_Password"].disabled = status; } }
@mplungjan Added the whole shabang with i starting at 1 in the loop. Make sense?
Remove the status from inside the loop or every second will be disabled
@mplungjan yes it will be disabled!!
No - every SECOND element will be disabled.
0

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;
  }
}

Comments

0

Have you tried out

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

Comments

0

You can do it like so:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.