Appending a variable's value to a variable name?

I am curious to know if there is a way of appending a variable’s value to the end of another variable’s name?

For instance, I have a variable, displayGrid that is assigned a value of 3 (this value can change). I also have variables: gridText1, gridText2, gridText3…

I want to use displayGrid’s value to append to another variable’s name. ie: gridText(the value of displayGrid here to make gridText3).

I’m modifying an application so creating arrays is not an option, unless there is absolutely no way to do this.

Thanks!

John

eval() function?

But how?

gridText+eval(displayGrid)

hmm…

I get an “undefined” returned when I alert it out.

alert(gridText+eval(displayGrid));

You can use square bracket notation.

Global variables are properties of the window object:


var displayGrid = 3;
alert(window["gridText" + displayGrid]);

^ Much better than using eval.

I have a follow up question to this one. (BTW, thanks, guys)

I have the following function:


        ////////////////////////////////////////////////////////
        //  This function will submit the form to the
        //    appropriate script
        //
        function submitForm(status, [COLOR=Red]nameOfForm[/COLOR])
        {
        alert("nameOfOFrom: " + nameOfForm);
          switch(status)
          {
            case "Canceled":
              document.[COLOR=Red]eval(nameOfForm)[/COLOR].action = "a.cancelBag.php";
              document.[COLOR=Red]eval(nameOfForm)[/COLOR].submit();
              break;
              
            default:
              document.[COLOR=Red]eval(nameOfForm)[/COLOR].action = "a.changeStatus.php";
              document.[COLOR=Red]eval(nameOfForm)[/COLOR].submit();
              break;
          }
          return;
        }

The second properties (I guess it’s called a property) that is highlighted in red above changes based on the form that is submitted (at least it’s supposed to). This is not working, and this is the error that I’m getting back with the above code:


document.eval is not a function

Is there some way to do dynamically populate that second property with a value passed to the function, or is there another way?

eval is not a property of the document. It’s a global function (you use it by itself).

Also, you should avoid using it. Use the square bracket notation along with the window object as mentioned by r51 earlier on. That is, of course, if this is necessary. Is nameOfForm simply the name attribute of something? If that’s the case, all you need to use is getElementsByName instead.

The nameOfForm is a variable who’s value is the value of the “name” attribute of the form. On that particular page I have multiple forms and I need to identify the correct one.

As far as the using the bracket notation, what would that look like? I’ve not ever used it before, so how would you rewrite one of those lines using it?

Thanks!

This didn’t work, but I’m not too surprised, either. :slight_smile:

window[nameOfForm].submit();
document.getElementsByName(nameOfForm)[0].submit()

window[nameOfForm] would refer to a variable that has the same value as nameOfForm. These are all identical:

window.foo = 'bar';

window['foo'] = 'bar';

var x = 'foo', y = 'bar';
window[x] = y; // note: window.x = y would not be the same thing.

var foo = 'bar'; // only in global scope (not in a function)

foo = 'bar'; // always global (normally can be avoided)

Thanks so much!