eval() not working 
July 20th, 2005, 11:07 AM
| | | 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. | 
July 20th, 2005, 11:07 AM
| | | 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/ | 
July 20th, 2005, 11:07 AM
| | | 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 | 
July 20th, 2005, 11:07 AM
| | | 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;
} | 
July 20th, 2005, 11:08 AM
| | | 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] | 
July 20th, 2005, 11:08 AM
| | | 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] | 
July 20th, 2005, 11:08 AM
| | | 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] | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,662 network members.
|