Thread: 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!!
Advertisements

About Dave Winer

Dave Winer, 54, pioneered the development of weblogs, syndication (RSS), podcasting, outlining, and web content management software; former contributing editor at Wired Magazine, research fellow at Harvard Law School, entrepreneur, and investor in web media companies. A native New Yorker, he received a Master's in Computer Science from the University of Wisconsin, a Bachelor's in Mathematics from Tulane University and currently lives in Berkeley, California.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s