Connecting Tech Pros Worldwide Help | Site Map

eval() not working

  #1  
Old July 20th, 2005, 12:07 PM
Bill
Guest
 
Posts: n/a
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, 12:07 PM
Martin Honnen
Guest
 
Posts: n/a

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, 12:07 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a

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, 12:07 PM
Lee
Guest
 
Posts: n/a

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, 12:08 PM
Bill
Guest
 
Posts: n/a

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, 12:08 PM
Bill
Guest
 
Posts: n/a

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, 12:08 PM
Bill
Guest
 
Posts: n/a

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]


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
divContent scroll not working in mozilla PrabodhanP answers 3 April 29th, 2009 08:58 PM
JS function is not working in IE AMT India answers 4 October 8th, 2007 12:54 PM
execute() not working iulian.ilea@gmail.com answers 6 October 22nd, 2006 01:35 PM
eval() not working in firefox Angel answers 4 June 14th, 2006 11:55 PM