Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

eval() not working

Question posted by: Bill (Guest) on July 20th, 2005 11:07 AM
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.




Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
July 20th, 2005
11:07 AM
#2

Re: eval() not working


Bill wrote:
[color=blue]
> 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;
> }[/color]

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/


Thomas 'PointedEars' Lahn's Avatar
Thomas 'PointedEars' Lahn
Guest
n/a Posts
July 20th, 2005
11:07 AM
#3

Re: eval() not working
Bill wrote:
[color=blue]
> [...]
> 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.[/color]

'cause that's fantasy syntax.

Try this:

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

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


PointedEars


Lee's Avatar
Lee
Guest
n/a Posts
July 20th, 2005
11:07 AM
#4

Re: eval() not working
Bill said:[color=blue]
>
>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?[/color]

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;
}


Bill's Avatar
Bill
Guest
n/a Posts
July 20th, 2005
11:08 AM
#5

Re: eval() not working
Excellent... thanks!


"Lee" <REM0VElbspamtrap@cox.net> wrote in message
news:bmh800014er@drn.newsguy.com...[color=blue]
> Bill said:[color=green]
> >
> >Hi
> >
> >I have a form called 'store' with many fields, and I can update the[/color][/color]
'name'[color=blue][color=green]
> >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?[/color]
>
> 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;
> }
>[/color]



Bill's Avatar
Bill
Guest
n/a Posts
July 20th, 2005
11:08 AM
#6

Re: eval() not working
Cheers!



"Martin Honnen" <mahotrash@yahoo.de> wrote in message
news:3f8c2383$1@olaf.komtel.net...[color=blue]
>
>
> Bill wrote:
>[color=green]
> > Hi
> >
> > I have a form called 'store' with many fields, and I can update the[/color][/color]
'name'[color=blue][color=green]
> > 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;
> > }[/color]
>
> 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/
>[/color]



Bill's Avatar
Bill
Guest
n/a Posts
July 20th, 2005
11:08 AM
#7

Re: eval() not working
Consider eval forgotten. Thanks.


"Thomas 'PointedEars' Lahn" <PointedEars@web.de> wrote in message
news:3F8C23FD.5090202@PointedEars.de...[color=blue]
> Bill wrote:
>[color=green]
> > [...]
> > 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.[/color]
>
> 'cause that's fantasy syntax.
>
> Try this:
>
> function setStoreValue(f, v)
> {
> document.forms['store'].elements[f].value = v;
> }
>[color=green]
> > This eval function has me beat. Is this the only way to set this up?[/color]
>
> eval(...) is evil[tm]. In most cases you will not need but
> avoid this method. Use the collection properties instead.
>
>
> PointedEars
>[/color]



 
Not the answer you were looking for? Post your question . . .
189,086 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors