Skip to content

Instantly share code, notes, and snippets.

@bor
Created July 10, 2012 13:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bor/3083170 to your computer and use it in GitHub Desktop.
Save bor/3083170 to your computer and use it in GitHub Desktop.
jQuery plugin: add .serializeObject() method to jQuery
// jQuery plugin: add .serializeObject() method to jQuery
// at this moment (jQuery v1.7.2) where no such method in core
// this method serializes a form into an (arguably more useful) object
(function($,undefined) {
$.fn.serializeObject = function() {
var obj = {};
$.each( this.serializeArray(), function(i,o) {
obj[o.name] = obj[o.name] === undefined ? o.value :
$.isArray( obj[o.name] ) ? obj[o.name].concat( o.value ) :
[ obj[o.name], o.value ];
});
return obj;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment