Skip to content

Instantly share code, notes, and snippets.

@ElmahdiMahmoud
Created June 23, 2014 07:41
Show Gist options
  • Save ElmahdiMahmoud/27ffde57b61637cf3cca to your computer and use it in GitHub Desktop.
Save ElmahdiMahmoud/27ffde57b61637cf3cca to your computer and use it in GitHub Desktop.
jquery: serializeObject
$.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() {
$('#demo-form').submit(function() {
$('#output-demo-from').html(JSON.stringify($(this).serializeObject(),null,'\t'));
$(this).next().removeClass('hidden');
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment