473,703 Members | 2,644 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.stor e." + f + ".value") = v;
}

but when I do this, nothing happens - no errors, nothing.

Even this doesn't work:

function setStoreNameVal ue(n) {
eval("document. store.name.valu e = " + 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 15012


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.stor e." + 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.stor e." + 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.stor e." + f + ".value") = v;
}

but when I do this, nothing happens - no errors, nothing.

Even this doesn't work:

function setStoreNameVal ue(n) {
eval("document. store.name.valu e = " + 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.c om...
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.stor e." + f + ".value") = v;
}

but when I do this, nothing happens - no errors, nothing.

Even this doesn't work:

function setStoreNameVal ue(n) {
eval("document. store.name.valu e = " + 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*******@yaho o.de> wrote in message
news:3f******** @olaf.komtel.ne t...


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.stor e." + 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*********@we b.de> wrote in message
news:3F******** ******@PointedE ars.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.stor e." + 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1905
by: robcarlton | last post by:
hi everybody I've written this function to make a list of all of an objects attributes and methods (not for any reason, I'm just learning) def list_members(obj) l = dir(obj) return map(lambda x : eval('obj.'+x), l) but I get an error saying that obj isn't defined. As I understand it eval has access to the current local namespace, which does contain
9
5555
by: Jim Washington | last post by:
I'm still working on yet another parser for JSON (http://json.org). It's called minjson, and it's tolerant on input, strict on output, and pretty fast. The only problem is, it uses eval(). It's important to sanitize the incoming untrusted code before sending it to eval(). Because eval() is evil http://blogs.msdn.com/ericlippert/archive/2003/11/01/53329.aspx apparently in every language. A search for potential trouble with eval() in...
6
2113
by: Roebie | last post by:
Hi everyone, I'm having some weird problem with evaluating the continue statement. Within a for loop I'm trying to evaluate a string (generated somewhere earlier) which basically has the continue statement in it. IE6 seems to have major problems with that as it generates an error "Can't have 'continue' outside of loop". Does anyone know why and/or have a workaround? I haven't tried any other browser since this one is the only one...
18
3156
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My question is what happens to the dynamically created assembly when the method is done running? Does GC take care of it? Or is it stuck in RAM until the ASP.Net process is recycled? This code executes pretty frequently (maybe 4 times per transaction) and...
4
9029
by: Angel | last post by:
Hello Everybody, I have the following lines in my code 1) totalElements=eval("document."+formname+".RCBillingCycle"+totalRCRows+".length") 2) var newoption=new Option (BillingCycleName,BillingCycleId); eval("document."+formname+".RCBillingCycle"+totalRCRows+".options= newoption");
3
5739
by: nishac | last post by:
My code is to change the frequency display as each radiobutton is pressed. ie,when radio button for daily is pressed corresponding data displays when radio for weekly pressed....etc The code works well in IE but not in firefox.I hope its the problem with eval() If i put the code can anyone please help me to find a better solution. <input name="dayType" type="radio" value="Y" onClick="dontShowthis('4');" <?=$checked4?>> //there are 4...
6
5920
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with or without parms. I know that any function that is passed to Eval() must be declared Public. It can be a Sub or Function, as long as it's Public. I even have it where the "function" evaluated by Eval can be in a form (class) module or in a standard...
10
494
by: Gordon | last post by:
I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); objType is a select with the different types of objects you can create as values. I really don't like using eval, and it's causing problems, like if I do something like the following:
0
5993
by: =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post by:
I have a listview that when I select an item, it populates a details view. I want to show the item that was selected in the listview by changing it to yellow. Trouble is, the selected item does not show as yellow until I've clicked the same item two times. What am I doing wrong on the selected item that keeps it from changing to yellow until the item is clicked two times? Also, I have to set the listviews selectedIndex in the...
1
4170
by: MistryP | last post by:
Hey Everyone, I m developing website in ASP.net and i am using Hyperlink control in this website but it is not working in Firefox browser but working in Internet Explorer. My code is: <table border="0" width="1000px"> <tr> <td style="height:20px" align="left"> <asp:Repeater ID ="RepReceipeTitle" runat="server"> <ItemTemplate>
0
8658
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6583
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5919
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4416
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4674
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2419
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.