Subject: 1/2 yearly clearance

From: Local Auto Specials <[email-masked]>
Date: Fri, 09 May 2014 12:06:31 -0700

Automotive Digest - May 2014
............................

Now's the time to select a 2014 model - Dealerships clearing lots

You can find a better deal on a 2014 model... Then on a used vehicle.

Updated list of 2014's that must go
-> http://www.poddap.com/may/auto/clearance/incentive.index

As of May 9th, 2014 Available Incentives: $0 Down, 0% APR, $1,000 Bonus Cash,
View Complete list of Incentives - http://www.poddap.com/may/auto/clearance/incentive.index 
* we can not guarantee all incentives 

- modify message settings w/ Urgent Info Services 
three two zero five Lake Sarah Independence, MN 55359 http://www.poddap.com/30f/no33.skf545 because the first returns a string, exactly like what you'd get if you submitted the form with a GET method, and the second gives you a array of objects, each with a name value pair. I want that if i have a field named "email" i get an object that will allow me to retrieve that value with obj.email. With serializeArray(), i'd have to do something like obj[indexOfElement].value   Yisroel Jul 26 '09 at 14:05
1	  	
@Taylor I don't see any answer on there that addresses the specific question better than Tobias' answer below.   James McCormack Dec 22 '10 at 12:41
2	  	
@James - The accepted answer using D. Crockford's JSON-js library. Here's an example: github.com/tleese22/google-app-engine-jappstart/blob/master/src/   Taylor Leese Dec 22 '10 at 20:11
2	  	
@Taylor Yes, I'd say the correct answer uses Crockford's lib and Tobias' function like so: JSON.stringify(('myForm').serializeObject())   James McCormack Dec 23 '10 at 12:30
3	  	
@Jonz - There are other reasons besides submission/transmission for using a form element. If you're doing any heavy lifting with the form values within JavaScript (e.g. single page app), it's very handy to have them in an object format for accessing & manipulating. Also, HTTP Post and Get query strings aren't the only formats for moving data around.   Patrick M Jul 9 '12 at 14:11
show 5 more comments
36 Answers
activeoldestvotes
1 2 next
up vote
690
down vote
accepted
serializeArray already does exactly that, you just need to massage the data into your required format:
fn.serializeObject = function(){ var o = {};
var a = this.serializeArray();
each(a, function() {
if (o[this.name] == undefined) {
if (o[this.name].push) {
o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } });return o;Example of this in action: Fiddle
Ok, I know this already has a highly upvoted answer, but another similar question was asked recently, and I was directed to this question as well. I'd like to offer my solution as well, because it offers an advantage over the accepted solution: You can include disabled form elements (which is sometimes important, depending on how your UI functions)Here is my answer from the other SO question:Initially, we were using jQuery's serializeArray() method, but that does not include form elements that are disabled. We will often disable form elements that are "sync'd" to other sources on the page, but we still need to include the data in our serialized object. So serializeArray() is out. We used the :input selector to get all input elements (both enabled and disabled) in a given container, and then $.map() to create our object.
as tvanfosson says, why iterate over the collection twice?   Yisroel Jul 27 '09 at 16:15
28	  Do you mean "why use serializeArray to get the data in the first place?" Because serializeArray is already written, is unit tested in multiple browsers, and could theoretically be improved in later versions of jQuery. The less code you write that has to access inconsistent things like DOM elements directly, the more stable your code will be.   Tobias Cohen Jul 28 '09 at 3:05Be warned, serializeArray() will not include disabled elements. I often disable input elements that are sync'd to other elements on the page, but I still want them included in my serialized object. You're better off using something like .map( ("#container :input"), function(n, i) { /* n.name and (n).val() */ } ); if you need to include disabled elements.   Samuel Meacham Jul 18 '10 at 23:54 
@TobiasCohen It doesn't handle foo[bar]-type inputs as expected, not to mention most of the other input name varieties. After being very frustrated with shallow solutions to this problem, I ended up writing my own jQuery plugin -- details in the answer I provided to this question.   maek Dec 7 '11 at 20:13
2	 @macek I know this is a few months old, but since when did do arrays use non-numeric indexes? No one should name an input foo[bar] and hope to treat it as an array. Are you confusing arrays and hashes? Yes, [] is commonly understood to be an accessor but not just for arrays. Also saying it's valid HTML but not in the HTML spec is a contradiction. Yes, the browser may not choke on it, but not many webservers are going to know how to deserialize that like they would an array. Why? Because its not in the HTML spec. Therefore, it is indeed invalid.   Kroehre Sep 8 '12 at 8:29
show 24 more comments
up vote
164
down vote
Convert forms to JSON LIKE A BOSS
UPDATE: The following code is now deprecated. You can find the current version on GitHub.
The following code can take work with all sorts of input names; and handle them just as you'd expect.