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 name by string manipulation

Status
Not open for further replies.

nkamp

Programmer
Joined
Jan 19, 2004
Messages
35
Location
NL
Hello,

When I start it seems to me that it is quit simple but now after a few hours I'am posting a thread.
I've two different frames and I want dynamically read the value's with a for loop in a different frame by object name. See my code example:

Code:
alert(top.frames['hiddenframe'].contexthiddenform.afkorting.value);
this works.

Now I want to make the field name (afkorting) dynamically
Code:
strName = (top.frames['hiddenframe'].document.contexthiddenform.elements[i].name);

alert(top.frames['hiddenframe'].document.contexthiddenform. + strName + .value);
This construction isn't working. The variable strName gets the name of the object (i.e. afkorting) but the alert doesn't work.
Has somebody an idea what I'am doing wrong?

Thanks in advance,

Nico
 
Your alert is not the same as the strName construct.

If the result of top.frames['hiddenframe'].document.contexthiddenform.elements.name is actually the name of the element you want, then, to get its value, form the construct in the same fashion:

top.frames['hiddenframe'].document.contexthiddenform.elements[[red]strname[/red]].value

The concatentation function for Strings (using the + sign) works when concatentating string literals (things in quotes) or variables. The problem is that document.myForm.specificElement.value is this weird thing where the elements are literal, but not in quotes, so the normal rules of String concatenation do not apply.

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
If you access forms and elements as an associative array, you shouldn't have any problem:

Code:
strName = (top.frames['hiddenframe'].document.contexthiddenform.elements[i].name);

alert(top.frames['hiddenframe'].document.contexthiddenform.elements[strName].value);

Lee
 
I'm just a bit short on the draw today compared to you, Dave. :)#

Lee
 
[rofl]

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top