By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,598 Members | 1,894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,598 IT Pros & Developers. It's quick & easy.

Question on creating variables on the fly

P: n/a
Hi, I'm pretty new to javascript and was wondering how you would piece
together a variable name and then assign it a value. I want to create a
hidden field and assign it a value based on the value of another variable

Right now it looks like:

("document.all.SM_MARK_10" + dateNumber + ".value")

where dateNumber is an already defined integer. What I want is to say

document.all.SM_MARK_1001.value="XX"
document.all.SM_MARK_1002.value="X2"

etc.

based on that value of dateNumber. Any way to do this easily?


Jul 20 '05 #1
Share this Question
Share on Google+
22 Replies


P: n/a
Lee
Tom Moroow said:

Hi, I'm pretty new to javascript and was wondering how you would piece
together a variable name and then assign it a value. I want to create a
hidden field and assign it a value based on the value of another variable

Right now it looks like:

("document.all.SM_MARK_10" + dateNumber + ".value")

where dateNumber is an already defined integer. What I want is to say

document.all.SM_MARK_1001.value="XX"
document.all.SM_MARK_1002.value="X2"

etc.

based on that value of dateNumber. Any way to do this easily?


It sounds like there must be a better way to do whatever it is
that you're actually trying to accomplish.

One solution might be to have two hidden fields already in the
form, named "VARNAME" and "VARVALUE", and assign your computed
name to the first and the value to the second. Use names that
are more meaningful in your application, though.

It would help to know what you want to do. Your terminology is
off a bit, since form fields are not the same as variables.

You should also find a new reference manual that doesn't teach
you to use "document.all", since that is out of date and never
worked on all browsers.

Jul 20 '05 #2

P: n/a

"Tom Moroow" <tr**@socal.rr.com> wrote in message
news:Pv*******************@newssvr29.news.prodigy. com...
Hi, I'm pretty new to javascript and was wondering how you would piece
together a variable name and then assign it a value. I want to create a
hidden field and assign it a value based on the value of another variable

Right now it looks like:

("document.all.SM_MARK_10" + dateNumber + ".value")

where dateNumber is an already defined integer. What I want is to say

document.all.SM_MARK_1001.value="XX"
document.all.SM_MARK_1002.value="X2"

etc.

based on that value of dateNumber. Any way to do this easily?


Use eval(string)

Jul 20 '05 #3

P: n/a
"Tom Moroow" <tr**@socal.rr.com> wrote in message news:<Pv*******************@newssvr29.news.prodigy .com>...

Hi, I'm pretty new to javascript and was wondering how you would piece
together a variable name and then assign it a value. I want to create a
hidden field and assign it a value based on the value of another variable

Right now it looks like:

("document.all.SM_MARK_10" + dateNumber + ".value")

where dateNumber is an already defined integer. What I want is to say

document.all.SM_MARK_1001.value="XX"
document.all.SM_MARK_1002.value="X2"

etc.

based on that value of dateNumber. Any way to do this easily?

Here's how you piece together an Element's ID with out hardcoding it.
Make sure you use the element's ID rather then the NAME attribute.
<input type="button" ID="myID" value="Hello"
onClick="findValue('ID')">

<script>
function findValue(strElementID)
{
//Find and Read the value
alert(document.getElementById('my'+strElementID).v alue);

//Find and Change the value
document.getElementById('my'+strElementID).value = "Goodbye";
}
</script>
Jul 20 '05 #4

P: n/a
"Brian" <Br**********@nospam.yahoo.com> wrote in message
news:3f******@10.10.0.241...
<snip>
("document.all.SM_MARK_10" + dateNumber + ".value")

where dateNumber is an already defined integer. What I want is to say

document.all.SM_MARK_1001.value="XX"
document.all.SM_MARK_1002.value="X2"
<snip>Use eval(string)


The *tiny* sub-set of JavaScript programming in which the use of eval is
appropriate:-

<URL: http://www.jibbering.com/faq/#FAQ4_40 >

- does not ever include dynamic property accessor construction as that
is facilitated (both more reliably and more efficiently) with the
standard bracket notation property accessor syntax:-

<URL: http://www.jibbering.com/faq/#FAQ4_39 >

Richard.
Jul 20 '05 #5

P: n/a
Richard Cornford wrote:
The *tiny* sub-set of JavaScript programming in which the use of
eval is appropriate [...] does not ever include dynamic property
accessor construction as that is facilitated (both more reliably
and more efficiently) with the standard bracket notation property ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ accessor syntax:- ^^^^^^^^^^^^^^^
Or shorter: The member operator[1] `[...]' :)
<URL: http://www.jibbering.com/faq/#FAQ4_39 >


http://devedge.netscape.com/library/...r.html#1008750
PointedEars
___________
[1] Previously falsely addressed by me as the `index operator'.
--
The last words of an *n*x admin:
# fsck /dev/hda8
/dev/hda8 ist already mounted. Destroy directory structure anyway?<y> yes
Please stand by...
Jul 20 '05 #6

P: n/a
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:3F**************@PointedEars.de...
<snip>
... the standard bracket notation property

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
accessor syntax:-

^^^^^^^^^^^^^^^
Or shorter: The member operator[1] `[...]' :)


That is one term that could be used but ECMA 262 section 11.2.1 is
headed "Property Accessors" and I would rather use the terminology from
the standard.

Richard.
Jul 20 '05 #7

P: n/a
Tom,

You may need to look at the eval() function.
Here's a short example

<html
<script>
dateNumber = "01";
eval("SM_MARK_10"+dateNumber+"='XX'");
alert(SM_MARK_1001); // Displays XX
</script>
</html>

You can use it to reference named fields in your document in the sort of way
you mentioned as in the following example:

<html>

<body>
<input id="SM_MARK_1001" value=6></input>
</body>

<script>
dateNumber = "01";
newValue = 28;
eval("document.all.SM_MARK_10"+dateNumber+".value= "+newValue);
</script>

</html>

Health warning. Use this knowledge with caution as it becomes very easy to
write WORN code (Write Once Read Never).

Regards,
Chris.

"Tom Moroow" <tr**@socal.rr.com> wrote in message
news:Pv*******************@newssvr29.news.prodigy. com...
Hi, I'm pretty new to javascript and was wondering how you would piece
together a variable name and then assign it a value. I want to create a
hidden field and assign it a value based on the value of another variable

Right now it looks like:

("document.all.SM_MARK_10" + dateNumber + ".value")

where dateNumber is an already defined integer. What I want is to say

document.all.SM_MARK_1001.value="XX"
document.all.SM_MARK_1002.value="X2"

etc.

based on that value of dateNumber. Any way to do this easily?

Jul 20 '05 #8

P: n/a
"Chris" <c_********@btconnect.com> writes:

Please don't top post!
You may need to look at the eval() function.
.... but most likely not.

<URL:http://jibbering.com/faq/#FAQ4_40> ("When should I use eval?")
<script>
(the type attribute is required in HTML)
dateNumber = "01";
eval("SM_MARK_10"+dateNumber+"='XX'");
Or:
this["SM_MARK_10"+dateNumber]='XX';

(since this script is running in the global scope, "this" refers to
the global object which holds the global variables as properties)
alert(SM_MARK_1001); // Displays XX You can use it to reference named fields in your document in the sort of way
you mentioned as in the following example:
By "named field", I assume you mean elements with an id?
<input id="SM_MARK_1001" value=6></input>
</body>

<script>
(scripts are not valid outside of the head and body tags. Put it
before </body>)
dateNumber = "01";
newValue = 28;
eval("document.all.SM_MARK_10"+dateNumber+".value= "+newValue);
The document.all collection is a proprietary Microsoft invention. The
W3C version is the document.getElementById function.

In either case, eval is *definitly* not needed. Us either:

document.all["SM_MARK_10"+dateNumber].value = newValue;

or:

document.getElementById("SM_MARK_10"+dateNumber).v alue = newValue;

(also consider how your version would work if newValue="text" -
because it wouldn't).

Health warning. Use this knowledge with caution as it becomes very easy to
write WORN code (Write Once Read Never).


Eval should be avoided, except by experienced programmers who know
when to use it. In far the most cases, it is not needed, and there
are better (safer, more efficient, more readable) ways.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #9

P: n/a
Lee
Chris said:

Tom,

You may need to look at the eval() function.
Here's a short example

<html
<script>
dateNumber = "01";
eval("SM_MARK_10"+dateNumber+"='XX'");
alert(SM_MARK_1001); // Displays XX
</script>
</html>

You can use it to reference named fields in your document in the sort of way
you mentioned as in the following example:

<html>

<body>
<input id="SM_MARK_1001" value=6></input>
</body>

<script>
dateNumber = "01";
newValue = 28;
eval("document.all.SM_MARK_10"+dateNumber+".value= "+newValue);
</script>

</html>

Health warning. Use this knowledge with caution as it becomes very easy to
write WORN code (Write Once Read Never).


In fact, it should absolutely never be used in that way.
Please read the FAQ.
http://www.jibbering.com/faq

Jul 20 '05 #10

P: n/a
Lee,
I stand corrected.

Tom,
To be FAQ compliant I think I should have suggested:
document.getElementById("SM_MARK_10"+dateNumber).v alue=newValue;
instead of eval() for document elements.

I'm not sure there's a more respectable alternative for variables (example
1 - the subject of your question).
Maybe Lee can assist here.

Regards,
Chris.
"Lee" <RE**************@cox.net> wrote in message
news:br*********@drn.newsguy.com...
Chris said:

Tom,

You may need to look at the eval() function.
Here's a short example

<html
<script>
dateNumber = "01";
eval("SM_MARK_10"+dateNumber+"='XX'");
alert(SM_MARK_1001); // Displays XX
</script>
</html>

You can use it to reference named fields in your document in the sort of wayyou mentioned as in the following example:

<html>

<body>
<input id="SM_MARK_1001" value=6></input>
</body>

<script>
dateNumber = "01";
newValue = 28;
eval("document.all.SM_MARK_10"+dateNumber+".value= "+newValue);
</script>

</html>

Health warning. Use this knowledge with caution as it becomes very easy towrite WORN code (Write Once Read Never).


In fact, it should absolutely never be used in that way.
Please read the FAQ.
http://www.jibbering.com/faq

Jul 20 '05 #11

P: n/a
Lee
Chris said:

Lee,
I stand corrected.

Tom,
To be FAQ compliant I think I should have suggested:
document.getElementById("SM_MARK_10"+dateNumber).v alue=newValue;
instead of eval() for document elements.

I'm not sure there's a more respectable alternative for variables (example
1 - the subject of your question).
Maybe Lee can assist here.


window["SM_MARK_10"+dateNumber]

But if you're having to create variables on the fly, you probably
need to rethink the design. You should probably be using arrays
or objects.

Jul 20 '05 #12

P: n/a
Lee wrote:
window["SM_MARK_10"+dateNumber]


Provided that the underlying markup was

<body>
<input id="SM_MARK_1001" value=6></input>
</body>

this will work only with the IE browser component, implementing members
of the document.all collection as properties of the `window' object.
PointedEars
Jul 20 '05 #13

P: n/a
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:br************@ID-107532.news.uni-berlin.de...
window["SM_MARK_10"+dateNumber]


Provided that the underlying markup was

<body>
<input id="SM_MARK_1001" value=6></input>
</body>

this will work only with the IE browser component,
implementing members of the document.all collection
as properties of the `window' object.


No, Chris's challenge relates to creating JavaScript variables on the
fly and Lee's answer exploits the fact that JavaScript global variables
are also properties of the global (window, on web browsers) object.
Lee's response is intended to be a non-eval alternative for -
eval("SM_MARK_10"+dateNumber+"='XX'"); - as a method for creating a
global variable and in the context of its use:-

window["SM_MARK_10"+dateNumber] = 'XX';

-is an equally effective and more efficient alternative.

Richard.
Jul 20 '05 #14

P: n/a
Richard Cornford wrote:
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:br************@ID-107532.news.uni-berlin.de...
window["SM_MARK_10"+dateNumber]


Provided that the underlying markup was

<body>
<input id="SM_MARK_1001" value=6></input>
</body>

this will work only with the IE browser component,
implementing members of the document.all collection
as properties of the `window' object.


No, Chris's challenge relates to creating JavaScript variables on the
fly and Lee's answer exploits the fact that JavaScript global variables
are also properties of the global (window, on web browsers) object.
Lee's response is intended to be a non-eval alternative for -
eval("SM_MARK_10"+dateNumber+"='XX'"); - as a method for creating a
global variable and in the context of its use:-

window["SM_MARK_10"+dateNumber] = 'XX';

-is an equally effective and more efficient alternative.

Richard.


I'm starting to use this:

// outside of any function or code block
var global = this;
// anywhere in the script
global["SM_MARK_10" + dateNumber] = 'XX';

As per your suggestion I believe.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #15

P: n/a
Richard Cornford wrote:
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
window["SM_MARK_10"+dateNumber]

Provided that the underlying markup was

<body> <input id="SM_MARK_1001" value=6></input> </body>

this will work only with the IE browser component, implementing members
of the document.all collection as properties of the `window' object.


No, Chris's challenge relates to creating JavaScript variables on the fly
and Lee's answer exploits the fact that JavaScript global variables are
also properties of the global (window, on web browsers) object. Lee's
response is intended to be a non-eval alternative for -
eval("SM_MARK_10"+dateNumber+"='XX'"); - as a method for creating a
global variable and in the context of its use:-

window["SM_MARK_10"+dateNumber] = 'XX';

-is an equally effective and more efficient alternative.


Of course it is in *some* (most?) UAs, but you have missed

,-<br**********@titan.btinternet.com>
| You can use it to reference named fields in your document
| in the sort of way you mentioned as in the following example:
|
| [See above.]

It is to be noted and watched for that global variables (which are also
properties of the `window' object as it is a property of the global object
referring to its owner in recent HTML UAs [ECMAScript], including the IE
browser component) and members of the `document.all' collection share the
same namespace in the DOM of the IE browser component.
PointedEars
___________
[ECMAScript]
http://www.ecma-international.org/pu...s/Ecma-262.htm
Jul 20 '05 #16

P: n/a
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:br************@ID-107532.news.uni-berlin.de...
<snip>
window["SM_MARK_10"+dateNumber] = 'XX';

-is an equally effective and more efficient alternative.
Of course it is in *some* (most?) UAs,


To date nobody has named an HTML UA that does not have a property of the
global object named "window" that is a reference to the global object.
Some other types of UA (e.g. SVG) do not have global "window" properties
but Grant has already posted one method of referencing the global object
in a way that depends only on the language instead of the
UA/environment.
but you have missed

,-<br**********@titan.btinternet.com>
| You can use it to reference named fields in your document <snip>

No I did not, it has ceased to be relevant as Chris had recognised that
he did not need - eval - to access DOM elements and had only challenged
Lee to present a method of creating global variables. So Lee's response
fully addressed that question without any dependence beyond the
requirement that it be used in an HTML UA and is certainly not dependent
on underlying mark-up or execution on IE as your post asserted.
It is to be noted and watched for that global
variables ( [ ... ] ) and members of the `document.all'
collection share the same namespace in the DOM of
the IE browser component.


What on earth are you talking about now?

Richard.
Jul 20 '05 #17

P: n/a
"Richard Cornford" <Ri*****@litotes.demon.co.uk> writes:
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:br************@ID-107532.news.uni-berlin.de...
It is to be noted and watched for that global
variables ( [ ... ] ) and members of the `document.all'
collection share the same namespace in the DOM of
the IE browser component.


What on earth are you talking about now?


That IE makes global variables out of named elements' names?

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #18

P: n/a
Richard Cornford wrote:
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
<snip>
window["SM_MARK_10"+dateNumber] = 'XX';

-is an equally effective and more efficient alternative.


Of course it is in *some* (most?) UAs,


[...]
but Grant has already posted one method of referencing the global object
in a way that depends only on the language instead of the
UA/environment.


This is true *if*, and only if, by accessing a property of the global object
a *global* *variable* is meant and _not_ a reference to a named DOM object.
Otherwise Grant's method is highly dependent on the JavaScript host as
well, i.e. not working in other than the IE browser component.
It is to be noted and watched for that global
variables ( [ ... ] ) and members of the `document.all'
collection share the same namespace in the DOM of
the IE browser component.


What on earth are you talking about now?


The same as before, see Lasse's abstract of the above.
PointedEars
Jul 20 '05 #19

P: n/a
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:br************@ID-107532.news.uni-berlin.de...
<snip>
window["SM_MARK_10"+dateNumber] = 'XX';

-is an equally effective and more efficient alternative.

Of course it is in *some* (most?) UAs,
[...]
but Grant has already posted one method of referencing the
global object in a way that depends only on the language
instead of the UA/environment.


This is true *if*, and only if, by accessing a property of
the global object a *global* *variable* is meant


Exactly how many times do I have to write "global variable" in this
thread before it is clear that I mean global variable?
and _not_ a reference to a named DOM object.

<snip>

And how often do I need to explain that the DOM element question has
been resolved and is no longer relevant?

Richard.
Jul 20 '05 #20

P: n/a
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:7k**********@hotpop.com...
<snip>
It is to be noted and watched for that global
variables ( [ ... ] ) and members of the `document.all'
collection share the same namespace in the DOM of
the IE browser component.


What on earth are you talking about now?


That IE makes global variables out of named elements' names?


The fact that some named members of the document.all collection share
the same property name as some properties of the global object on IE
(and imitators) does not seem to suite the description "share the same
namespace".

Richard.
Jul 20 '05 #21

P: n/a
Richard Cornford wrote:
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
<snip>
>window["SM_MARK_10"+dateNumber] = 'XX';
>
>-is an equally effective and more efficient alternative.

Of course it is in *some* (most?) UAs,

[...]
but Grant has already posted one method of referencing the
global object in a way that depends only on the language
instead of the UA/environment.


This is true *if*, and only if, by accessing a property of
the global object a *global* *variable* is meant


Exactly how many times do I have to write "global variable" in this
thread before it is clear that I mean global variable?


You can repeat it a hundred times if you like, it does not make my argument
invalid :) I referred to the example Lee used and have merely added the
point as a note to Grant's solution.
and _not_ a reference to a named DOM object.

<snip>

And how often do I need to explain that the DOM element question has
been resolved and is no longer relevant?


So you declare it resolved and thus it is resolved? Funny.
PointedEars
Jul 20 '05 #22

P: n/a
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:br************@ID-107532.news.uni-berlin.de...
<snip>
This is true *if*, and only if, by accessing a property of
the global object a *global* *variable* is meant


Exactly how many times do I have to write "global variable"
in this thread before it is clear that I mean global variable?


You can repeat it a hundred times if you like, it does not make
my argument invalid :) I referred to the example Lee used and
have merely added the point as a note to Grant's solution.


Lee's example was posted in response to a message that read:-

<quote cite="http://groups.google.com/groups?selm=
brqhlr%2480%241%40hercules.btinternet.com">
Lee,
I stand corrected.

Tom,
To be FAQ compliant I think I should have suggested:
document.getElementById("SM_MARK_10"+dateNumber).v alue=newValue;
instead of eval() for document elements.

I'm not sure there's a more respectable alternative for variables
(example 1 - the subject of your question).
Maybe Lee can assist here.
^^^^^^^^^^^^^^^^^^^^^^^^^
</quote>

-and the referenced "example 1" reads:-

<quote cite="http://groups.google.com/groups?selm=
brq9ji%24jbu%241%40titan.btinternet.com">
<html
<script>
dateNumber = "01";
eval("SM_MARK_10"+dateNumber+"='XX'");
alert(SM_MARK_1001); // Displays XX
</script>
</html>
</quote>

- clearly the creation of a global variable, as is evident from the
assignment of a string value to the variable with the constructed name.

So from Lee's response on the subject under discussion is the creation
of global variables and any limitations in browser support for accessing
DOM elements as named properties of the global object using similar
syntax is not relevant.
and _not_ a reference to a named DOM object.

<snip>

And how often do I need to explain that the DOM element
question has been resolved and is no longer relevant?


So you declare it resolved and thus it is resolved? Funny.


It wasn't me who decided it was resolved it was Chris.

Richard.
Jul 20 '05 #23

This discussion thread is closed

Replies have been disabled for this discussion.