Skip to content

Instantly share code, notes, and snippets.

@roden0
Created May 24, 2013 14:20
Show Gist options
  • Save roden0/5643850 to your computer and use it in GitHub Desktop.
Save roden0/5643850 to your computer and use it in GitHub Desktop.
Convert form data to JS object with jQuery. http://www.tobiascohen.com/
$.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;
};
$(function() {
$('form').submit(function() {
$('#result').text(JSON.stringify($('form').serializeObject()));
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment