Skip to content

Instantly share code, notes, and snippets.

@adardesign
Created March 5, 2013 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adardesign/5091258 to your computer and use it in GitHub Desktop.
Save adardesign/5091258 to your computer and use it in GitHub Desktop.
someNameSpace = {
//...
processForm: function (form) {
return JSON.stringify(form.serializeObject(function (key, val) {
return {
name: key,
value: encodeURIComponent(val)
};
}));
}
//...
};
$.fn.serializeObject = function(fn) {
var o = {},
a = this.serializeArray(),
that;
$.each(a, function() {
that = this;
if($.isFunction(fn)){
that = fn(that.name, that.value);
}
if(o[this.name] !== undefined) {
val = that.value || '';
if(!o[that.name].push) {
o[that.name] = [o[that.name]];
}
o[that.name].push(that.value);
} else {
o[that.name] = that.value;
}
});
return o;
};
@adardesign
Copy link
Author

jquery.serializeObject.js is similar to $.serializeAray but it turns in the form data into a js object (which is easily converted to JSON by pasing it in to JSON.stringify())

A callback function is optional to all to manipulate/process the form data..
A typical usage would be to encodeURIComponent..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment