Skip to content

Instantly share code, notes, and snippets.

@solepixel
Last active October 26, 2018 19:03
Show Gist options
  • Save solepixel/a02ee4896e49f6d88f08dc06313cd311 to your computer and use it in GitHub Desktop.
Save solepixel/a02ee4896e49f6d88f08dc06313cd311 to your computer and use it in GitHub Desktop.
Serialize form inputs
$.fn.serializeObject = function() {
var arrayData = this.serializeArray(),
objectData = {};
$.each( arrayData, function() {
var value;
if ( this.value != null ) {
value = this.value;
} else {
value = '';
}
this.name = this.name.replace( '[]', '' );
if ( objectData[ this.name ] != null ) {
if ( ! objectData[ this.name ].push ) {
objectData[ this.name ] = [ objectData[ this.name ] ];
}
objectData[ this.name ].push( value );
} else {
objectData[ this.name ] = value;
}
});
return objectData;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment