Skip to content

Instantly share code, notes, and snippets.

@jeff-hager-dev
Created August 18, 2015 06:36
Show Gist options
  • Save jeff-hager-dev/0f13dbc7f054e7ac6b78 to your computer and use it in GitHub Desktop.
Save jeff-hager-dev/0f13dbc7f054e7ac6b78 to your computer and use it in GitHub Desktop.
Using Jquery to convert a form to an Object representation of it.
//source: answer to http://stackoverflow.com/questions/1184624/convert-form-data-to-javascript-object-with-jquery
$.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;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment