473,406 Members | 2,345 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,406 software developers and data experts.

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 1170
You can use the eval function....

var somVar ="SomeValue";

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

eval(Elementname);
alert(yourNameSomeValue);

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...@web.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...@web.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.getElementById('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...@gmail.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.javascript message <876f5ddc-6d98-4995-b0d0-8c8e110522e5@s5
0g2000hsb.googlegroups.com>, Sat, 10 May 2008 07:31:42, Laser Lips
<lo*********@gmail.composted:
>You can use the eval function....

var somVar ="SomeValue";

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

eval(Elementname);
alert(yourNameSomeValue);
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.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jun 27 '08 #7
DL
On May 10, 6:45*pm, VK <schools_r...@yahoo.comwrote:
On May 10, 7:53 am, DL <tatata9...@gmail.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.getElementById('tbl');
var tr1 = newF.insertRow();

would work. But

var newF = document.getElementById('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...@gmail.comwrote:
On May 10, 6:45*pm, VK <schools_r...@yahoo.comwrote:


On May 10, 7:53 am, DL <tatata9...@gmail.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.getElementById('tbl');
var tr1 = newF.insertRow();

would work. But

var newF = document.getElementById('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
VK
On May 11, 7:32 am, DL <tatata9...@gmail.comwrote:
e.g. newCell1.style.align = "right";
align="left/right/center" is an attribute of the cell itself, not a
CSS rule.
Either:
newCell.align = 'right';
or
newCell.style.textAlign = 'right';
// the latter is lesser functional than the first one

Jun 27 '08 #11
DL wrote:
On May 10, 10:34 am, Thomas 'PointedEars' Lahn <PointedE...@web.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.
The problem is that you are apparently resistant to studying for yourself.
You will not achieve anything until you solve this problem.
Here's more detail:

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

The above code failed.
A useless error description. See also <http://jibbering.com/faq/#FAQ4_43>.

And that it failed is unlikely, since it is syntactically correct since
JavaScript 1.3 (NN 4), JScript 2.0 (NT 4), ECMAScript Ed. 3. It is more
likely that you don't know what you are doing, and therefore you have used
it wrong.

What replaces the ellipsis matters here. You may have expected `a[1]' to be
accessed as `a1' later which is not the case. My solution provides you with
a way to let go of several numbered variables and to use an array data
structure instead.
A related question, how to increment a value?
The following won't work
See above.
// the i value is param for a function like the above one, not be
concerned
var rowCount = 1;
rowCount = eval(rowCount + i);
RTFM. The above *equals* the most simple

rowCount = rowCount + i;

or

rowCount += i;

That is, provided `i' is a number value. If it is not, you will observe NaN
as result or string concatenation instead (e.g. rowCount === "14" if i ===
"4"). To convert a value to the number type explicitly, there are several ways.

RTFFAQ: http://jibbering.com/faq/#FAQ4_21
alert (rowCount);
Should be

window.alert(rowCount);
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jun 27 '08 #12
VK wrote:
On May 11, 7:32 am, DL <tatata9...@gmail.comwrote:
>e.g. newCell1.style.align = "right";

align="left/right/center" is an attribute of the cell itself, not a
CSS rule.
Obviously you don't know what a CSS rule is:

http://www.w3.org/TR/CSS2/syndata.html#q8
Either:
newCell.align = 'right';
or
newCell.style.textAlign = 'right';
// the latter is lesser functional than the first one
^^^^^^^^^^^^^^^^^
Considering we will celebrate CSS2's 10th birthday tomorrow, how did you get
that idea?
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 27 '08 #13
DL
On May 11, 3:36*am, VK <schools_r...@yahoo.comwrote:
On May 11, 7:32 am, DL <tatata9...@gmail.comwrote:
e.g. newCell1.style.align = "right";

align="left/right/center" is an attribute of the cell itself, not a
CSS rule.
Either:
* newCell.align = 'right';
or
* newCell.style.textAlign = 'right';
* // the latter is lesser functional than the first one
Thank you.
Jun 27 '08 #14

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

Similar topics

3
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....
6
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...
2
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...
3
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...
0
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...
1
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...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
18
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...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
0
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,...
0
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...

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.