473,670 Members | 2,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it possible to create dynamic var

DL

For instance,

function addNew(i) {
var 'element'&i
...
}

If possible, how to?

Thanks.
Jun 27 '08 #1
13 1186
You can use the eval function....

var somVar ="SomeValue" ;

var Elementname = "var yourName" + somVar + "='Hello';" ;

eval(Elementnam e);
alert(yourNameS omeValue);

Graham
Jun 27 '08 #2
DL wrote:
For instance,

function addNew(i) {
var 'element'&i
..
}

If possible, how to?
While eval() is a possibility, and a bad one at that, chances
are that you really don't need what you want. Consider this:

function addNew(i)
{
var a = [];
a[i] = 42;
...
}
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jun 27 '08 #3
On May 10, 3:34*pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
DL wrote:
For instance,
function addNew(i) {
var 'element'&i
..
}
If possible, how to?

While eval() is a possibility, and a bad one at that, chances
are that you really don't need what you want. *Consider this:

* function addNew(i)
* {
* * var a = [];
* * a[i] = 42;
* * ...
* }

PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
What alternatives are there?

Jun 27 '08 #4
DL
On May 10, 10:34*am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
DL wrote:
For instance,
function addNew(i) {
var 'element'&i
..
}
If possible, how to?

While eval() is a possibility, and a bad one at that, chances
are that you really don't need what you want. *Consider this:

* function addNew(i)
* {
* * var a = [];
* * a[i] = 42;
* * ...
* }
Thank you both. I kind of like the above approach, it does not seem
to solve the problem.
Here's more detail:

function addNew(i) {
var newF = document.getEle mentById('tbl') ;
var a = [];
a[i] = newF.insertRow( );
// ...
}

The above code failed. A related question, how to increment a value?
The following won't work
// the i value is param for a function like the above one, not be
concerned
var rowCount = 1;
rowCount = eval(rowCount + i);
alert (rowCount);
Jun 27 '08 #5
VK
On May 10, 7:53 am, DL <tatata9...@gma il.comwrote:
For instance,

function addNew(i) {
var 'element'&i
..

}

If possible, how to?
Javascript is not a prehistoric BASIC subset for such perversions. If
you need uniformly accessible elements of unknown in advance amount
then use array:

var myElements = new Array;

function addNew(i) {
myElements[i] = whatever;
}

If you really need properties named like "foo1', "foo2" etc. then use
object's squared brackets notation:

var myElements = new Object;

function addNew(i) {
myElements['foo'+i] = whatever;
}
Jun 27 '08 #6
In comp.lang.javas cript message <876f5ddc-6d98-4995-b0d0-8c8e110522e5@s5
0g2000hsb.googl egroups.com>, Sat, 10 May 2008 07:31:42, Laser Lips
<lo*********@gm ail.composted:
>You can use the eval function....

var somVar ="SomeValue" ;

var Elementname = "var yourName" + somVar + "='Hello';" ;

eval(Elementna me);
alert(yourName SomeValue);
If you had read the newsgroup FAQ carefully, and its Notes, you would
have known better than to suggest that.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK. ?@merlyn.demon. co.uk IE7 FF2 Op9 Sf3
news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jun 27 '08 #7
DL
On May 10, 6:45*pm, VK <schools_r...@y ahoo.comwrote:
On May 10, 7:53 am, DL <tatata9...@gma il.comwrote:
For instance,
function addNew(i) {
var 'element'&i
..
}
If possible, how to?

Javascript is not a prehistoric BASIC subset for such perversions. If
you need uniformly accessible elements of unknown in advance amount
then use array:

var myElements = new Array;

function addNew(i) {
*myElements[i] = whatever;

}

If you really need properties named like "foo1', "foo2" etc. then use
object's squared brackets notation:

var myElements = new Object;

function addNew(i) {
*myElements['foo'+i] = whatever;

}- Hide quoted text -

- Show quoted text -
Thanks. First let me get simple thing off the list first, your

var myElements = new Array;
is probably similar to
var myElements = []; // the later is a short form probably, yes?

Now, back to the main topic. Also, I should have been clearer about
the intent of the code, that is, to dynamically add TR or (TRs) for an
existing table with ID of 'tbl'.

Neither Array nor Object works for this case while
by doing so by hand, e.g.
var newF = document.getEle mentById('tbl') ;
var tr1 = newF.insertRow( );

would work. But

var newF = document.getEle mentById('tbl') ;
var myElements = new Array;

function addNew(i) {
myElements[i] = newF.insertRow( );

won't work.

It seems to be me pretty odd that javascript can't handle 'dynamic
variable assignment'

Thanks.
Jun 27 '08 #8
DL
On May 10, 10:06*pm, DL <tatata9...@gma il.comwrote:
On May 10, 6:45*pm, VK <schools_r...@y ahoo.comwrote:


On May 10, 7:53 am, DL <tatata9...@gma il.comwrote:
For instance,
function addNew(i) {
var 'element'&i
..
}
If possible, how to?
Javascript is not a prehistoric BASIC subset for such perversions. If
you need uniformly accessible elements of unknown in advance amount
then use array:
var myElements = new Array;
function addNew(i) {
*myElements[i] = whatever;
}
If you really need properties named like "foo1', "foo2" etc. then use
object's squared brackets notation:
var myElements = new Object;
function addNew(i) {
*myElements['foo'+i] = whatever;
}- Hide quoted text -
- Show quoted text -

Thanks. *First let me get simple thing off the list first, your

var myElements = new Array;
is probably similar to
var myElements = []; // the later is a short form probably, yes?

Now, back to the main topic. *Also, I should have been clearer about
the intent of the code, that is, to dynamically add TR or (TRs) for an
existing table with ID of 'tbl'.

Neither Array nor Object works for this case while
by doing so by hand, e.g.
var newF = document.getEle mentById('tbl') ;
var tr1 = newF.insertRow( );

would work. But

var newF = document.getEle mentById('tbl') ;
var myElements = new Array;

*function addNew(i) {
*myElements[i] = newF.insertRow( );

won't work.

It seems to be me pretty odd that javascript can't handle 'dynamic
variable assignment'

Thanks.- Hide quoted text -

- Show quoted text -
Ok, you guys are right, I don't even need dynamic vars. Now got a
minor question,
the following attempt of setting a newly created cell alignment to
right won't work,
e.g. newCell1.style. align = "right";
what's wrong?

Thanks.
Jun 27 '08 #9
DL wrote:
Ok, you guys are right, I don't even need dynamic vars. Now got a
minor question,
the following attempt of setting a newly created cell alignment to
right won't work,
e.g. newCell1.style. align = "right";
what's wrong?
Shouldn't it be
newCell1.style. textAlign = "right";

Andrew Poulos
Jun 27 '08 #10

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

Similar topics

3
2937
by: CAD Fiend | last post by:
Hello, Well, after an initial review of my database by my client, they have completely changed their minds about how they want their form. As a result, I'm having to re-think the whole process. My Current Form (6 tabs): - Owner, Property, Title, Docs, Queries, & Reports - User is able to see (while navigating through the tabs) above in the form : Owner Name, Address, Parcel, and SSN
6
1233
by: dw | last post by:
Hello, all. We're building a Web app where we can have any number of questions, which can contain any type of form element -- check boxes, text fields, radio buttons, dropdowns, etc. We'd like to build an admin page so someone can create the questions and store them on SQL Server. The responses from those questions need to then be stored on the database. Is this possible or is it just a pipe dream? If possible, is it so complicated that...
2
1163
by: natzol | last post by:
Just a question to verify - I am a beginner in .Net Our business needs require that we have to modify db tables on regular basis (add column, remove column, add/drop table etc). Therefore it is a pain to modify business logic layer+business data layer after you for example added a column to a database. Additionally you have to recompile after every change and bring a server down. Therefore is there a way to bypass re-compilation...
3
52331
by: ryanmhuc | last post by:
Is it possible to have a dynamic table name within a query or a table name that is a variable? This does not work but gives an example: SELECT * FROM concat('table', 'name') - OR - SET @table = 'a'; SELCT * FROM @table Of course the query can be constructed dynamically but does not exactly
0
1157
by: josur | last post by:
Hi, I would like to know if is possible to create dynamic reports based on cubes. What i mean is,after creating a cube with a couple of dimensions and measure if is there any way to give the normal users on the report manager or report builder the freedom to choose their own dimensions/measure so they can output the report with the choosen criteria. Thanks.
1
4652
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button ,another row will be created with the same control (I mean another dropdown and 2 button) and so on. and by pressing Remove button the selecetd row will be removed. I used viewstate to keep my value for postback, I want by changing selectedvalue of...
0
3493
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button ,another row will be created with the same control (I mean another dropdown and 2 button) and so on. and by...
18
2286
by: Angus | last post by:
Hello We have a lot of C++ code. And we need to now create a library which can be used from C and C++. Given that we have a lot of C++ code using classes how can we 'hide' the fact that it is C++ from C compilers? Can we have a C header file which uses the functionality of the C++ files and compile this into a lib file?
0
8471
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
8815
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
8592
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
8661
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
7421
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
5686
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
4213
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...
1
2802
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
2044
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.