472,119 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

eval() not working

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.


Jul 20 '05 #1
6 14942


Bill wrote:
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;

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
Bill wrote:
[...]
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

Jul 20 '05 #3
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;
}

Jul 20 '05 #4
Excellent... thanks!
"Lee" <RE**************@cox.net> wrote in message
news:bm*********@drn.newsguy.com...
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;
}

Jul 20 '05 #5
Cheers!

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:3f********@olaf.komtel.net...


Bill wrote:
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;

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #6
Consider eval forgotten. Thanks.
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:3F**************@PointedEars.de...
Bill wrote:
[...]
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

Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by robcarlton | last post: by
9 posts views Thread by Jim Washington | last post: by
6 posts views Thread by Roebie | last post: by
18 posts views Thread by Joe Fallon | last post: by
4 posts views Thread by Angel | last post: by
10 posts views Thread by Gordon | last post: by
reply views Thread by =?Utf-8?B?TWlrZSBDb2xsaW5z?= | 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.