Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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