D
DL
For instance,
function addNew(i) {
var 'element'&i
...
}
If possible, how to?
Thanks.
function addNew(i) {
var 'element'&i
...
}
If possible, how to?
Thanks.
DL said:For instance,
function addNew(i) {
var 'element'&i
..
}
If possible, how to?
DL said: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 = 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
You can use the eval function....
var somVar ="SomeValue";
var Elementname = "var yourName" + somVar + "='Hello';";
eval(Elementname);
alert(yourNameSomeValue);
DL said: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 = 42;
...
}
For instance,
function addNew(i) {
var 'element'&i
..
}
If possible, how to?
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 = 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 -
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 = 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 = 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 -
Shouldn't it beDL said: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?
e.g. newCell1.style.align = "right";
DL said:While eval() is a possibility, and a bad one at that, chancesDL said:For instance,
function addNew(i) {
var 'element'&i
..
}
If possible, how to?
are that you really don't need what you want. Consider this:
function addNew(i)
{
var a = [];
a = 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 = 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);
VK said: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
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
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.