473,698 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic variables

Dear All,

In short, this works:
function testEval(){
var veh = "car";
eval(veh + "Ford" + "models" + "= new
Array('Fiesta', 'Mondeo','Falco n');");
for(i = 0; i < 3; i++){
alert("The model: " + carFordmodels[i]);
}
}

This doesn't:

function testEval(){
var FordModels = new Array('Fiesta', 'Mondeo','Falco n');
var veh = "car";
eval(veh + "Ford" + "models" + "=" + FordModels);
for(i = 0; i < 3; i++){
alert("The model: " + carFordmodels[i]);
}
}

The browser (two of them) thinks Fiesta is an undefined variable. How
do I get the second example to work, i.e. where there is a reference to
an Array of Strings. I also tried eval(veh + "Ford" + "models") =
FordModels; but no luck.

I'm quite stuck.

Thanks in advance.

Chris

Jul 23 '05 #1
4 17305


Antonie C Malan Snr wrote:

function testEval(){
var FordModels = new Array('Fiesta', 'Mondeo','Falco n');
var veh = "car";
eval(veh + "Ford" + "models" + "=" + FordModels);
for(i = 0; i < 3; i++){
alert("The model: " + carFordmodels[i]);
}
}

The browser (two of them) thinks Fiesta is an undefined variable.


I am sure you don't need all those evals but I think what you are
looking for is
eval(veh + "Ford" + "models" + "= FordModels;");
as what you have yields
eval("carFordmo dels=Fiesta,Mon deo,Falcon")
and then the scripting engine rightly complaints that Fiesta is undefined.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Lee
Antonie C Malan Snr said:

Dear All,

In short, this works:
function testEval(){
var veh = "car";
eval(veh + "Ford" + "models" + "= new
Array('Fiesta' ,'Mondeo','Falc on');");
for(i = 0; i < 3; i++){
alert("The model: " + carFordmodels[i]);
}
}

This doesn't:

function testEval(){
var FordModels = new Array('Fiesta', 'Mondeo','Falco n');
var veh = "car";
eval(veh + "Ford" + "models" + "=" + FordModels);
for(i = 0; i < 3; i++){
alert("The model: " + carFordmodels[i]);
}
}

var dynamic=new Object();
function doNotUseEvalFor DynamicVariable s() {
var FordModels = ['Fiesta','Monde o','Falcon'];
var veh = "car";
dynamic[veh + "Ford" + "models"] = FordModels;
for(i = 0; i < 3; i++){
alert("The model: " + dynamic.carFord models[i]);
}
}

Jul 23 '05 #3
Antonie C Malan Snr wrote:
Dear All,

In short, this works:
function testEval(){
var veh = "car";
eval(veh + "Ford" + "models" + "= new
Array('Fiesta', 'Mondeo','Falco n');");
for(i = 0; i < 3; i++){
alert("The model: " + carFordmodels[i]);
}
}

This doesn't:

function testEval(){
var FordModels = new Array('Fiesta', 'Mondeo','Falco n');
var veh = "car";
eval(veh + "Ford" + "models" + "=" + FordModels);
for(i = 0; i < 3; i++){
alert("The model: " + carFordmodels[i]);
}
}

The browser (two of them) thinks Fiesta is an undefined variable. How
do I get the second example to work, i.e. where there is a reference to
an Array of Strings. I also tried eval(veh + "Ford" + "models") =
FordModels; but no luck.


Cheese and crackers! That is about the worst possible way to do that.
Objects do this much much more nicely. And here's a tip: If you are
using eval, you are doing it wrong.

var veh = {car: {}, truck: {}};
veh['car']["Ford" + "models"] = ['Fiesta', 'Mondeo', 'Galcon'];
var models = veh.car.Fordmod els;
for (i = 0; i < models.length; i += 1) {
alert("The model: " + models[i]'
}

http://www.crockford.com/javascript/survey.html
Jul 23 '05 #4


Chris Malan

Thanks guys. Now it works. What I wanted to do is create any number of
JavaScript arrays from data in the database. The array names are
dynamic, but can be got at from data in the selects. E.g. if somebody
wants to see all the Fords in the Category car the name of the
modelsArray will be carFordModels. As I don't know which makes are
going to be in the database I could think of no other way to do it. I
don't want a round trip for every select value selection to see what the
next select valid values are.

Thanks,

Chris

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2361
by: propizzy | last post by:
Appreciate any help!!! PROBLEM: I have this form that allows the user to dynamically create additional fields (see javascript code bellow). I am trying to retrieve the values entered into these fields from my php script. I ultimatly need to pick out the variables and place the values into my php script page. I have worked with a little code I found on php.net (see php code below), but I wan unable to grasp the knowledge on how to...
2
8405
by: Tommy Lang | last post by:
Hi everybody! I am trying to learn the basics of C++ myself and have a hard time understanding some stuff like pointers and references etc. I have created a small program that adds two numbers and prints the result on the screen. The program works just fine and I am proud of it :). But now I want to make some changes to it and only use dynamic variables (and make use of *, &, new...). I know from books that you create dynamic variables...
1
2239
by: Tommy Lang | last post by:
I am trying to learn to use dynamic variables. I have pasted the code below. Is this the proper way of using dynamic variables? Thanks, Tommy //------------------------------------------------------------ #include <iostream>
4
13062
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or column names in a FROM clause. After many hours or reading all manner of manuals I've discovered it appears this is not possible and that in order to so I need to further venture into dynamic SQL. My present procedure is based on all static SQL...
1
17664
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to Create a Dynamic Crosstab Report PRODUCT :Microsoft Access PROD/VER:1.00 1.10 OPER/SYS:WINDOWS
28
4622
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. Would the performance be better using Static versus Dynamic. I would think it would be quicker with STATIC declarations since the variables would only have to be created once. Can anyone confirm this. Thanks. -- Dennis in Houston
12
6246
by: scott | last post by:
Is there a way to create dynamic variables when looping through a recordset? For example below, after the 1st loop I'd have myVarA1 and myVarB1, after 2nd loop, I'd get myVarA2 and myVarB2. CODE *********************************** set objRS = GetMyRecordSet() i=1 objRS.MoveFirst
2
2937
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic sub-report will capture what grades the student has achieved in a list of different subjects and the reason I need it to be dynamic is that students take different subjects. Basically I've been trying to doctor the KB article on dynamic
2
6351
by: JWL | last post by:
Hi I need to create a bunch of sites with slightly dynamic CSS. Basically, all the image paths in the CSS need to be dynamic, depending on the values of certain ASP variables. I can think of 3 ways to do this: 1. Write a script to create a semi-dynamic CSS file, which will be run whenever we need to make changes to the ASP variables controlling the
3
1948
by: Mark S. | last post by:
As I understand it, C# doesn't offer dynamic variable names. Below is my attempted workaround. Is what I'm doing possible? FYI, I already read all the "why in the world do you need dynamic variabale names" comments by other posters, if you are kind enough to post a reply please either say it's impossible, or it is possible if I did x. TIA using System; namespace ConsoleForTesting
0
8674
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
8603
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,...
0
8861
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
7725
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
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
4369
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
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
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
2329
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.