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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String to object name 1

Status
Not open for further replies.

batfg

Technical User
Mar 4, 2009
2
GB
I have a script which grabs window.location, splits it and then puts part of it into a string. The script also defines a number of arrays.

I want to use the string to pull elements out of the associated array (assuming it exists). For example, if the string value is "X" I want to pull elements from the array that is called X, e.g. the elements X[0], X[1] etc.

The problem is that I can't simply write X[0] as X is a string. How can I relate the string value to the name of the array?
 
I think you can use eval to do what you want. I know at least that I use it to form whole JS statements from strings:
Code:
try {eval(document.jstest.ta1.value);}

_________________
Bob Rashkin
 
Thank you. After playing around with eval I managed to do it using eval(splitstring[1].valueOf())[0] where splitstring[1] contained the name of the array I was trying to get to.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top