473,395 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 51186
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: phancey | last post by:
I am trying to invoke a web service method dynamically. I have created a generic function that takes a method name, string of parameters and calls the web method using System.Reflection: ...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Kevin B Ebert | last post by:
Today, I ran into something I didn't quite expect. I'm sure there is a logical explanation for it so I want to post it and see if anyone can explain the difference to me. I came across a...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
3
by: James | last post by:
Has anyone written a utility to convert a C# form to C++.net? i.e. to convert "using System.Data" to "using namespace System::Data" etc
3
by: dale zhang | last post by:
Hi, I am trying to read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp The article author is using PictureBox for windows...
1
by: Daniel | last post by:
I have looked everywhere on the web for an answer to this and the only thing I can find is converting the image format when the file is present on the local filesystem. What I want to do is use a...
3
by: Mark Kamoski | last post by:
Hi-- What is the difference between Convert.ToString(obj) and CType(obj, String)? (Assume obj is a variable of type Object.) Please advise. Thank you.
7
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.