By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,678 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,678 IT Pros & Developers. It's quick & easy.

convert string to object reference - avoiding eval

P: 1
I was told on this site eval was bad but its the only way i can work out how to do it.. their example was
use var imdesc = document.myform[imagename1].value
not var imdesc = eval("document.myform." + imagename1 + ".value")
but i could not see how to change mine to emulate it...

I have a function displaytable() which i pass the id of a div into so:

displaytable(MyLayer1) - shows the first table hidden in the <div id=MyLayer1>
displaytable(MyLayer2) - shows the second table etc

So in my updatetemplate function I used eval to pass the string version of the object when the relevent menu item is selected:

Expand|Select|Wrap|Line Numbers
  1. function updatetemplate()
  2. {
  3.     var dropdown = document.form1.template;
  4.     var thenumber = dropdown.options[dropdown.selectedIndex].value;
  6.     eval ( 'displaytable(MyLayer' + thenumber + ')' ) 
  7. }

The function then passes the object and changes its display method
Expand|Select|Wrap|Line Numbers
  1. function displaytable(xxx)
  2. {
  3. = "block"
I'm not sure which bit to change.. I've tried lots of things and they all just make it fall over. The eval is the only way ive got it to work.

e.g. i tried this to no avail .. just get "expecting object" error:

Expand|Select|Wrap|Line Numbers
  1.     var convertit = document.['MyLayer'+thenumber]
  2.     displaytable( convertit ) ;
Jan 6 '06 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 15k+
P: 16,027
Expand|Select|Wrap|Line Numbers
  1. var div = document.getElementById("MyLayer"+thenumber);
  2. displaytable(div);
should work fine.
Oct 1 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.