I am pretty sure this is easy to figure out, but I don't know javascript that good. I am trying to declare variables dynamically but what I have below does not work.
What I want to accomplish is the following.
var id1 = document.getElementById("sid").value;
var id2 = document.getElementById("sid").value;
var id3 = document.getElementById("sid").value;

This is what I have so are but its not working.

var jscounter
for (i=0;i<=jscounter;i++)
{
var "id" + i + = document.getElementById("sid").value;
}

The above does not work. Help.

Recommended Answers

All 5 Replies

1. Read a book or blog on basic Javascript syntax.
2. You can't create variables in this manner. Instead create an array and add your values to that (your reading on basic syntax will help you here).
3. Your counter isn't initialized to have a value so your loop will not iterate.

1. Read a book or blog on basic Javascript syntax.
2. You can't create variables in this manner. Instead create an array and add your values to that (your reading on basic syntax will help you here).
3. Your counter isn't initialized to have a value so your loop will not iterate.

Thanks for the smart comments, but I did create the variables. The following code did exactly what I wanted. I miscopied the jscounter variable. I did set it to a number. I just didn't paste it. But thanks.

var jscounter=5;
var i=0;
var sid='sid';
for (i=0;i<=jscounter;i++)
{
eval("var id" + i + "=document.getElementById("+sid+").value;");
}

var id1 = document.getElementById("sid").value;
var id2 = document.getElementById("sid").value;
var id3 = document.getElementById("sid").value;

Dan,

First, just in case you dont know, the three statements will obtain exacltly the same value but I guess you have a reason.

OK, there are a few of ways to do what you want.

If id1 , id2 , id3 are global variables, then they are equivalent to window['id'+1] , window['id'+2] , window['id'+3] . This is so because (in the browser environment) all javascript global members (eg. variables, arrays, functions, objects) are properties of window.

If your vars are properties of some other object O , then they can be similarly addressed O['id'+1] , O['id'+2] , O['id'+3] .

Within a constructor function, you can similarly use this to create properties, eg :

for (i=1; i<=3; i++) {
  this["id" + i] = document.getElementById("sid").value;
}

where this stands for each particular instance of the constructor ("class" by any other name).

Local vars within a standard (non-constructor) function are different because they aren't properties of an accessible object in the same way as globals (without resorting to eval() which is kinda evil). The way round this is to create a local object (or associative array) to give yourself something that can be addressed, eg :

var O = {}; //object
/* or var O = []; for an array */
for (i=1; i<=3; i++) {
  O["id" + i] = document.getElementById("sid").value;
}

Airshow

Dan,

First, just in case you dont know, the three statements will obtain exacltly the same value but I guess you have a reason.

OK, there are a few of ways to do what you want.

If id1 , id2 , id3 are global variables, then they are equivalent to window['id'+1] , window['id'+2] , window['id'+3] . This is so because (in the browser environment) all javascript global members (eg. variables, arrays, functions, objects) are properties of window.

If your vars are properties of some other object O , then they can be similarly addressed O['id'+1] , O['id'+2] , O['id'+3] .

Within a constructor function, you can similarly use this to create properties, eg :

for (i=1; i<=3; i++) {
  this["id" + i] = document.getElementById("sid").value;
}

where this stands for each particular instance of the constructor ("class" by any other name).

Local vars within a standard (non-constructor) function are different because they aren't properties of an accessible object in the same way as globals (without resorting to eval() which is kinda evil). The way round this is to create a local object (or associative array) to give yourself something that can be addressed, eg :

var O = {}; //object
/* or var O = []; for an array */
for (i=1; i<=3; i++) {
  O["id" + i] = document.getElementById("sid").value;
}

Airshow

Thanks Airshow, I got a better understanding. Yeah I know they will have the same value.

They weren't "smart" comments they were answering what you posted. The way the question was posted was using incorrect syntax. An array syntax would serve you better. If you want help then post what you have not incomplete. Sorry if you think otherwise.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.