Click here to show or hide the menubar.
Thread started by Dave Winer on Friday, January 04, 2013.

Dynamic forms into Javascript objects?

I have a JavaScript question today.

I have a form with lots of input elements whose names I can't hard-code into the JavaScript. I have to be prepared for anything that might be there.

On a certain event I want to grab the values of all the input elements and put them into a JavaScript object, each as a property, with the name of the input becoming the name of the property.

I can get a nodelist of all the input elements in the form, as follows:

var inputs = document.getElementById ("myDialog").getElementsByTagName ("input");

Now, I have a global called myValues that I want to get all the values from the inputs into.

This is what I think the loop should look like (but it doesn't work).

for (var i = 0; i < inputs.length; i++) {

myValues [inputs [i].name] = inputs [i].value;

}

I've looked at various Stack Overflow pages, of course.

Any help much appreciated!

Update: It works. :-)

Here's the actual test app.

http://prefstest.blorkmark.com/

I think the mistake I was making was initializing appPrefs to [] when it should have been {}.

A picture named objectdump.gif

Still getting my wobbly JS sea legs to settle down. :-)

Thanks for all the help!!

XML