473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.


Jul 20 '05 #1
6 15002


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.store." + 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.store." + 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.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?


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.com...
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.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?


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*******@yahoo.de> wrote in message
news:3f********@olaf.komtel.net...


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.store." + 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*********@web.de> wrote in message
news:3F**************@PointedEars.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.store." + 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
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...
9
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...
6
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...
18
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...
4
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...
3
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...
6
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...
10
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...
0
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...
1
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: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.