eval() not working

B

Bill

Hi

I have a form called 'store' with many fields, and I can update the 'name'
field ok like this

document.store.name.value = n; //this works fine

but I want a function to update any field, so would like to do something
like this:

function setStoreValue(f, v) {
eval ("document.store." + f + ".value") = v;
}

but when I do this, nothing happens - no errors, nothing.

Even this doesn't work:

function setStoreNameValue(n) {
eval("document.store.name.value = " + n);
}


This eval function has me beat. Is this the only way to set this up?

Can anyone tell me how I'm doing this wrong?

Many thanks.
 
L

Lee

Bill said:
Hi

I have a form called 'store' with many fields, and I can update the 'name'
field ok like this

document.store.name.value = n; //this works fine

but I want a function to update any field, so would like to do something
like this:

function setStoreValue(f, v) {
eval ("document.store." + f + ".value") = v;
}

but when I do this, nothing happens - no errors, nothing.

Even this doesn't work:

function setStoreNameValue(n) {
eval("document.store.name.value = " + n);
}


This eval function has me beat. Is this the only way to set this up?

Don't use eval() for that. You've probably seen examples
of doing it, but those examples are wrong. Use:

function setStoreValue(f, v) {
document.store.elements[f].value = v;
}
 
M

Martin Honnen

Bill said:
Hi

I have a form called 'store' with many fields, and I can update the 'name'
field ok like this

document.store.name.value = n; //this works fine

but I want a function to update any field, so would like to do something
like this:

function setStoreValue(f, v) {
eval ("document.store." + f + ".value") = v;
}

Forget about eval, JavaScript 1.x allows you to use any expression to
access the property of an object if you use square brackets:
document.store[f].value = v;
 
T

Thomas 'PointedEars' Lahn

Bill said:
[...]
but I want a function to update any field, so would like to do something
like this:

function setStoreValue(f, v) {
eval ("document.store." + f + ".value") = v;
}

but when I do this, nothing happens - no errors, nothing.

'cause that's fantasy syntax.

Try this:

function setStoreValue(f, v)
{
document.forms['store'].elements[f].value = v;
}
This eval function has me beat. Is this the only way to set this up?

eval(...) is evil[tm]. In most cases you will not need but
avoid this method. Use the collection properties instead.


PointedEars
 
B

Bill

Excellent... thanks!


Lee said:
Bill said:
Hi

I have a form called 'store' with many fields, and I can update the 'name'
field ok like this

document.store.name.value = n; //this works fine

but I want a function to update any field, so would like to do something
like this:

function setStoreValue(f, v) {
eval ("document.store." + f + ".value") = v;
}

but when I do this, nothing happens - no errors, nothing.

Even this doesn't work:

function setStoreNameValue(n) {
eval("document.store.name.value = " + n);
}


This eval function has me beat. Is this the only way to set this up?

Don't use eval() for that. You've probably seen examples
of doing it, but those examples are wrong. Use:

function setStoreValue(f, v) {
document.store.elements[f].value = v;
}
 
B

Bill

Cheers!



Martin Honnen said:
Hi

I have a form called 'store' with many fields, and I can update the 'name'
field ok like this

document.store.name.value = n; //this works fine

but I want a function to update any field, so would like to do something
like this:

function setStoreValue(f, v) {
eval ("document.store." + f + ".value") = v;
}

Forget about eval, JavaScript 1.x allows you to use any expression to
access the property of an object if you use square brackets:
document.store[f].value = v;
 
B

Bill

Consider eval forgotten. Thanks.


Thomas 'PointedEars' Lahn said:
Bill said:
[...]
but I want a function to update any field, so would like to do something
like this:

function setStoreValue(f, v) {
eval ("document.store." + f + ".value") = v;
}

but when I do this, nothing happens - no errors, nothing.

'cause that's fantasy syntax.

Try this:

function setStoreValue(f, v)
{
document.forms['store'].elements[f].value = v;
}
This eval function has me beat. Is this the only way to set this up?

eval(...) is evil[tm]. In most cases you will not need but
avoid this method. Use the collection properties instead.


PointedEars
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top