n a
n a

Reputation: 2802

How to use a variable for document.form_name.element_name

Simple question. Is it possible to replace the following code:

document.form_name.element_name

with

document.form_name.<array[index].value>

?

Upvotes: 0

Views: 189

Answers (3)

Briguy37
Briguy37

Reputation: 8402

assuming <array[index].value> has "element_name" in it, you can do the following:

document.form_name[array[index].value]

Upvotes: 0

Halcyon
Halcyon

Reputation: 57729

var my_element = "username";
document.form_name[my_element];

and

var fields = [ "username", "realname" ];
document.form_name[fields[0]];

Upvotes: 0

Naftali
Naftali

Reputation: 146310

Try this:

document.form_name[array[index].value];

Upvotes: 4

Related Questions