473,796 Members | 2,599 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 15016


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
1913
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
5573
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
2121
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
3164
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
9035
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
5742
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
5932
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
6001
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
4177
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
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10187
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
10018
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...
0
6795
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
5446
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
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.