Connecting Tech Pros Worldwide Help | Site Map

eval() not working

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 11:07 AM
Bill
Guest
 
Posts: n/a
Default 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.





  #2  
Old July 20th, 2005, 11:07 AM
Martin Honnen
Guest
 
Posts: n/a
Default 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/

  #3  
Old July 20th, 2005, 11:07 AM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default 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

  #4  
Old July 20th, 2005, 11:07 AM
Lee
Guest
 
Posts: n/a
Default 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;
}

  #5  
Old July 20th, 2005, 11:08 AM
Bill
Guest
 
Posts: n/a
Default 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]


  #6  
Old July 20th, 2005, 11:08 AM
Bill
Guest
 
Posts: n/a
Default 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]


  #7  
Old July 20th, 2005, 11:08 AM
Bill
Guest
 
Posts: n/a
Default 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]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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.