Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added serializeObject per ajpiano's request

  • Loading branch information...
commit 15db5376bf3dd26e07b1d7b40c1cf66786a9fabc 1 parent 7d49db3
Ben Alman authored January 21, 2010
39  jquery.ba-serializeobject.js
... ...
@@ -0,0 +1,39 @@
  1
+/*!
  2
+ * jQuery serializeObject - v0.1 - 1/20/2010
  3
+ * http://benalman.com/
  4
+ * 
  5
+ * Copyright (c) 2010 "Cowboy" Ben Alman
  6
+ * Dual licensed under the MIT and GPL licenses.
  7
+ * http://benalman.com/about/license/
  8
+ */
  9
+
  10
+// Whereas .serializeArray() serializes a form into an array,
  11
+// .serializeObject() serializes a form into an (arguably more
  12
+// useful) object.
  13
+
  14
+(function($){
  15
+  
  16
+  $.fn.serializeObject = function(){
  17
+    var obj = {};
  18
+    
  19
+    $.each( this.serializeArray(), function(i,o){
  20
+      var n = o.name,
  21
+        v = o.value;
  22
+        
  23
+        obj[n] = obj[n] === undefined ? v
  24
+          : $.isArray( obj[n] ) ? obj[n].concat( v )
  25
+          : [ obj[n], v ];
  26
+    });
  27
+    
  28
+    return obj;
  29
+  };
  30
+  
  31
+})(jQuery);
  32
+
  33
+/*
  34
+
  35
+$('<form><input type="hidden" name="a" value="1"/><input type="hidden" name="a" value="2"/><input type="hidden" name="a" value="3"/><input type="hidden" name="b" value="4"/></form>').serializeObject();
  36
+
  37
+// returns { a: [ "1", "2", "3" ], b: "4" }
  38
+
  39
+*/
9  jquery.ba-serializeobject.min.js
... ...
@@ -0,0 +1,9 @@
  1
+/*
  2
+ * jQuery serializeObject - v0.1 - 1/20/2010
  3
+ * http://benalman.com/
  4
+ * 
  5
+ * Copyright (c) 2010 "Cowboy" Ben Alman
  6
+ * Dual licensed under the MIT and GPL licenses.
  7
+ * http://benalman.com/about/license/
  8
+ */
  9
+(function(a){a.fn.serializeObject=function(){var b={};a.each(this.serializeArray(),function(d,e){var f=e.name,c=e.value;b[f]=b[f]===undefined?c:a.isArray(b[f])?b[f].concat(c):[b[f],c]});return b}})(jQuery);

0 notes on commit 15db537

Please sign in to comment.
Something went wrong with that request. Please try again.