Dynamic variable names

I was wondering if it is possible to have dynamic variable names that would change according to the elements in a form for example. Here is some code I am playing with to accomplish this task. I’ve heard that I may be able to use the eval() function to accomplish this but am not exactly sure of the route to go, if this is possible at all.


var myForm = document.getElementById('myForm');

function getFormElements() {
     for (var i = 0; i < myForm.elements.length; i++) {
          var myForm.elements[i].id = document.getElementById(myForm.elements[i].value;
     }
}

Essentially what I would like to accomplish is calling a function that would scan the form for elements with ID’s. For each ID found it would create a variable with an identifier of that ID and a value of the element’s input.

Thanks for any suggestions

No, eval is evil. Do not use eval. Unless you’re someone like Douglas Crockford, there is always a better way than using eval.

Let’s use a simple form, as it’s always easier with an example.


<form id="personalInfo">
    <p><label for="firstname">First name: <input id="firstname" name="firstname" value="John"></label></p>
    <p><label for="lastname">Last name: <input id="lastname" name="lastname" value = "Hancock"></label></p>
</form>

My first comment for you is: why do you seem to want to duplicate the value within the form, when the id and form names are commonly the same?

Based on your example what I would like the function to do is create javascript variables for me called firstname and lastname and assign the user’s input as their values.

In other words I’d be trying to do something like this:


var firstname = document.getElementById('firstname').value;
var lastname = document.getElementById('lastname').value;


But rather than having the variables explicitly created I would like to have a generic function that can be used to accomplish the same task regardless of the inputs (firstname and lastname) so that it may be used in various scenarios without hardcoding the specific variables for each implementation.

Also, whats so evil about the eval() function?

http://blogs.msdn.com/b/ericlippert/archive/2003/11/01/53329.aspx
http://blogs.msdn.com/b/ericlippert/archive/2003/11/04/53335.aspx

I didn’t know it but eval() is considered evil in Ruby as well. Now the first link calls eval() slow, but I’ve heard from people who use it that it’s actually sometimes faster… so, faster than what? I guess if you’re writing a bunch of code to avoid eval() that ends up being molasses then maybe it’s worth looking into.

Based on your example what I would like the function to do is create javascript variables for me called firstname and lastname and assign the user’s input as their values.

You could use an associative array (not a real array).

var inputs = {
firstname : ‘Joe’;
lastname: ‘Smith’;
etc…
};

You’d start out with an empty object:
var inputs = {};

Here’s where I get stuck, I don’t know how to assign keys. I mean, you could type them yourself but it would be nice to have code that could go though any form, grab the input id’s and set them as keys, and then later loop through them and assign values the user filled in.

Here’s an idea: Doesn’t form.elements return an array-like collection similar to the above associative array?

Here’s an idea: Doesn’t form.elements return an array-like collection similar to the above associative array?

Hm I dunno, using .prototype property?

var formInputs = document.forms[‘someform’].elements; <–HtmlCollection but can it be like any Object?

for (i=0;i<formInputs.length; i++) {
formInputs.prototype.element[i].id=element[i].value;
}

dunno, that’s my guess until I’m behind my work computer.