Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Object Property Names

Status
Not open for further replies.

jmattturn

Programmer
Joined
Mar 27, 2006
Messages
2
Location
US
Forgive my ignorance, I am new to JS and I am trying to learn how to allow an Object Property Name to be modified by using a variable, but I am unsure if this is a) possible; and b) what the correct way to do it would be.

Say you have:

document.form1.select1.value = 10;

What I want to be able to do is have "select1" be a variable so I can loop through many instances of this in a script.

Any help would be greatly appreciated!
John
 
[tt]//x being any name for that matter, or a looping variable
var x="select1";
document.form1.elements[x].value=10;
[/tt]
 
You can set the form element itself to a variable using the following syntax:
Code:
var a = document.forms["form1"].elements["select1"];

//and then an example using the variable:

for (var i = 0; i < a.options.length; i++) {
   alert(a.options[i].value);
}

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Thank both of you so very much. I knew it had to be possible, I just wasn't familiar enough with the syntax.

Worked like a charm.

Thanks again,
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top