Is this the only way to do this?

C

Confused

Hi,

My background is more in C and PHP than javascript, but occasionally I
have to write small javascript functions. I recently needed to write
one where the parameter was the name of a form. The only way I could
find to assign the value of this form to a local variable was like
this:

function myFunction(formname)
{
var string = 'var f = document.' + formname;
eval(string);

This looks like a bit of a hack to me. Is there a better way to do
this?

Thanks.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
Hi,

My background is more in C and PHP than javascript, but occasionally I
have to write small javascript functions. I recently needed to write
one where the parameter was the name of a form. The only way I could
find to assign the value of this form to a local variable was like
this:

function myFunction(formname)
{
var string = 'var f = document.' + formname;
eval(string);

This looks like a bit of a hack to me. Is there a better way to do
this?

Your exact question was answered yesterday morning. But ISTM that toy
may well, but not necessarily, find it easy enough to pass in a
reference to the form (essentially a pointer, though not described as
such in the language) rather than the name of the form as a string.
That is easy to use within your myfunction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,373
Latest member
Desiree036

Latest Threads

Top