472,110 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

convert string to object reference

I know there's an easy answer to this which is escaping me:
--------------------------------------------------------------------------
-----------------------
I have clickable thumb images which pass the image info to a window open script
to display a larger version of the image along with a description of the image.
The image info comes from the image element; the description string is in a
hidden form var with the same name as the image element.

// example:
<a... <image name='imagename1' ... >/a>
<input type='hidden' name='imagename1' value='image descriptor1' .....

Problem:
inserting the image name argument into document.myform.imagename1.value
to get the value of the hidden field yields a string, not the value of that
form field.

Doing this:
var imdesc = "document.myform." + imagename1 + ".value"

yields a string var called imdesc with a value of
"document.myform.imagename1.value"

But I don't want that string, I want its object reference value.

There is something very basic here I'm overlooking... so the question is:
How do I convert the string to have it literally interpreted as the value of
the document.form + imagename1 argument ??

Many many thanks in advance,

Jim

Jul 20 '05 #1
5 51053
In article <20***************************@mb-m23.aol.com>,
ji*******@aol.comNoSpam enlightened us with...
var imdesc = "document.myform." + imagename1 + ".value"


var imdesc = document.myform[imagename1].value

-------------------------------------------------
~kaeli~
Hey, if you got it flaunt it! If you don't, stare
at someone who does. Just don't lick the TV screen,
it leaves streaks.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #2
try

var imdesc = eval("document.myform." + imagename1 + ".value")
"JimMenees" <ji*******@aol.comNoSpam> wrote in message
news:20***************************@mb-m23.aol.com...
I know there's an easy answer to this which is escaping me:
--------------------------------------------------------------------------
-----------------------
I have clickable thumb images which pass the image info to a window open script to display a larger version of the image along with a description of the image.

The image info comes from the image element; the description string is in a hidden form var with the same name as the image element.

// example:
<a... <image name='imagename1' ... >/a>
<input type='hidden' name='imagename1' value='image descriptor1' .....

Problem:
inserting the image name argument into document.myform.imagename1.value
to get the value of the hidden field yields a string, not the value of that form field.

Doing this:
var imdesc = "document.myform." + imagename1 + ".value"

yields a string var called imdesc with a value of
"document.myform.imagename1.value"

But I don't want that string, I want its object reference value.

There is something very basic here I'm overlooking... so the question is:
How do I convert the string to have it literally interpreted as the value of the document.form + imagename1 argument ??

Many many thanks in advance,

Jim

Jul 20 '05 #3
> try

var imdesc = eval("document.myform." + imagename1 + ".value")


No! Don't try that. That is extremely bad style. The correct way to write it is

var imdesc = document.myform[imagename1].value;

It is almost always incorrect to use the eval() function.

http://www.crockford.com/javascript/survey.html

Jul 20 '05 #4
Please tell me why ?
"Douglas Crockford" <no****@laserlink.net> wrote in message
news:bk**********@sun-news.laserlink.net...
try

var imdesc = eval("document.myform." + imagename1 + ".value")
No! Don't try that. That is extremely bad style. The correct way to write

it is
var imdesc = document.myform[imagename1].value;

It is almost always incorrect to use the eval() function.

http://www.crockford.com/javascript/survey.html

Jul 20 '05 #5
> > > try
var imdesc = eval("document.myform." + imagename1 + ".value")
No! Don't try that. That is extremely bad style.
The correct way to write it is var imdesc = document.myform[imagename1].value; It is almost always incorrect to use the eval() function. See http://www.crockford.com/javascript/survey.html
Please tell me why ?


That is a fair question. The faq (http://jibbering.com/faq/#FAQ4_40) recommends
avoiding eval(), but doesn't say why. There are lots of good reasons to avoid
it.

First, it is a crutch. It allows weak programmers to remain weak programmers by
avoiding learning about simple language features such as subscript notation.
JavaScript is a complete programming language. It is always better to use the
language's features correctly. It is always worse to synthesize features with
eval().

eval() is expensive. It invokes the JavaScript compiler every time it is called.

It has portability problems. Some interpreters vary in the context that eval()
operates in.

It makes it harder to reason about the correctness of a program. Verification
tools (http://www.crockford.com/javascript/lint.html) cannot validate the
correctness of an eval() expression.

Error detection is postponed from compile time to run time. This makes testing
more difficult and increases the likelihood that your errors will be shown to
users.

eval() can fail if the expression is not syntactically correct. The security
properties of eval() are horrible.

There are lots of very bad JavaScript books out there that routinely use eval().
If you have a bad one, chuck it in the trash right now and go out and get a good
one.

When you are reviewing scripts and libraries, routinely reject code that makes
inapporpriate use of eval(). Almost all uses are inappropriate.

Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Jim Bancroft | last post: by
3 posts views Thread by James | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.