Skip to content

Instantly share code, notes, and snippets.

@forivall
Created October 1, 2013 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forivall/6772931 to your computer and use it in GitHub Desktop.
Save forivall/6772931 to your computer and use it in GitHub Desktop.
jQuery.serializeObject
jQuery.fn.serializeObject = function() {
var target = {};
var source = this.serializeArray();
jQuery.each(source, function(i, item) {
if (target[item.name]) {
// multiple values of same name: coerce as array
if (!target[item.name].push) {
target[item.name] = [target[item.name]];
}
target[item.name].push(item.value || '');
} else {
target[item.name] = item.value || '';
}
});
return target;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment