views:

89

answers:

2

I'd like to be able to create a javascript object from my html form and am wondering if there's a good way to do this using jquery. I guess what i'm looking for is something similar to $.serialize, but which would result in a map instead of a string.

<form>
  <input type="text" name="foo1" value="bar1" />
  <input type="text" name="foo2" value="bar2" />
</form>

desired result:

{ foo1:"bar1", foo2:"bar2" }
A: 

How about serializeArray() http://api.jquery.com/serializeArray/

Update: Also found this plugin, which does essentially the same as the other poster's answer, but it looks like it can handle nested arrays. http://github.com/cowboy/jquery-misc/blob/master/jquery.ba-serializeobject.js

Sandro
That's not quite the same.
SLaks
No, I suppose not. My mistake.
Sandro
+2  A: 
var oElements = {};
$('form [name]').each(function(){
    oElements[this.name] = this.value;
});
Jabes88