0

I want to use a function's variable value to build a variable in the function.

Here's what I got so far:

function foo(1a, 1b) {
 var money = document.myform.1b;
}

So basically, if I pass in foo(aaa, bbb), I would like the variable money to be:

var money = document.myform.bbb;

1
  • Take note that 1a and 1b are not valid Identifiers. Commented Nov 24, 2010 at 5:40

3 Answers 3

2

You can use document.myform[bbb] if bbb is a string. Which effect are you after?

1

var bbb="variablename";
var money = document.myform.bbb

or 2

var bbb="variablename";
var money=document.myform.variablename
Sign up to request clarification or add additional context in comments.

1 Comment

document.myform[bbb] worked! I'm using this to make dynamic selects.
1
function foo(a, b) {
  var money = document.myform[b];
}

Also, I don't believe js vars can begin with a number.

Comments

-1

var money = document.getElementById(1b);

http://www.javascript-coder.com/javascript-form/getelementbyid-form.htm

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.